如何创建BinaryOperator以添加BigInteger

问题描述 投票:0回答:1

我想创建一个BinaryOperator<BigInteger> biOp以求和BigInteger值。例如,我将有一个巨大的列表或数组,其中包含不同的BigInteger值,我想使用循环和biOp将它们全部加起来。

例如两个值应如下所示:

System.out.println(biOp.apply(BigInteger.ONE, BigInteger.ONE));
// outputs 2

如何正确创建或初始化biOp

java add operator-keyword biginteger
1个回答
4
投票

最简单的方法是使用对BigInteger::add的方法引用:

BigInteger::add

这是有效的,因为当您使用类名创建对实例方法的方法引用(即不是BinaryOperator<BigInteger> binOp = BigInteger::add; 方法)时,static方法将为该实例使用一个额外的参数来调用该方法。因此,尽管apply方法采用一个add参数,但此方法引用采用了两个BigInteger参数。

© www.soinside.com 2019 - 2024. All rights reserved.