我想创建一个BinaryOperator<BigInteger> biOp
以求和BigInteger
值。例如,我将有一个巨大的列表或数组,其中包含不同的BigInteger
值,我想使用循环和biOp
将它们全部加起来。
例如两个值应如下所示:
System.out.println(biOp.apply(BigInteger.ONE, BigInteger.ONE));
// outputs 2
如何正确创建或初始化biOp
?
最简单的方法是使用对BigInteger::add
的方法引用:
BigInteger::add
这是有效的,因为当您使用类名创建对实例方法的方法引用(即不是BinaryOperator<BigInteger> binOp = BigInteger::add;
方法)时,static
方法将为该实例使用一个额外的参数来调用该方法。因此,尽管apply
方法采用一个add
参数,但此方法引用采用了两个BigInteger
参数。