Convenient functions of operators in Java 8?
•
Java
In Python, if I want to collapse the XOR operation, I can write:
reduce(operator.xor,my_things,0)
Not more trouble
reduce(lambda x,y: x^y,0)
Is there anything similar in the new Java 8 features? For example, write something like this
myThings.reduce(0,Integer::xor)
instead of
myThings.reduce(0,(x,y) -> x ^ y)
Solution
As you suggested in the private package intpipeline, integer #sum (int, int) is used, but there is no similar method of other numeric operators
@Override public final int sum() { return reduce(0,Integer::sum); }
You can define them yourself
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码