Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java method reference for arithmetic addition?

When using streams in Java, is it possible to use a method reference for arithmetic addition (rather than a lambda)?

At the moment (to sum the contents of a stream) I am using stream.reduce(0, (x, y) -> x + y), but I would like to use something like stream.reduce(0, Math::add), if it exists.


On a more general but similar note, does every method/operation in Java have a method reference?

like image 900
Tom C. Avatar asked Jan 20 '20 22:01

Tom C.


People also ask

What is the method reference in Java?

Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.

WHAT ARE method references a special type of Java?

Method references are a special type of lambda expressions. They're often used to create simple lambda expressions by referencing existing methods.

What is the advantage of using method reference in Java 8?

That's all about what is method reference is in Java 8 and how you can use it to write clean code in Java 8. The biggest benefit of the method reference or constructor reference is that they make the code even shorter by eliminating lambda expression, which makes the code more readable.

Why method reference is better than lambda?

The method references can only be used to replace a single method of the lambda expression. A code is more clear and short if one uses a lambda expression rather than using an anonymous class and one can use method reference rather than using a single function lambda expression to achieve the same.

What are arithmetic operators in Java?

These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators. Now let’s look at each one of the arithmetic operators in Java: 1.

What is addition Java?

Java program to print or calculate addition of two numbers with sample outputs and example programs.

Which method is used to add two numbers in Java?

1) addition (int x, int y) is the static method, which calculates the addition of two numbers and returns the value. 2) addition (a,b) method calls at the main method then that static method calculates the addition of two numbers and returns the value and value will be assigned to the variable c.

How many arguments does first add () method have in Java?

First add () method has two arguments which means when we pass two numbers while calling add () method then this method would be invoked. Similarly when we pass three and four arguments, the second and third add method would be invoked respectively.


2 Answers

Yes you can use Integer.sum,

stream.reduce(0, Integer::sum)

Or you can convert the stream to an IntStream and just call sum

stream.mapToInt(Integer::intValue).sum();
like image 126
Sleiman Jneidi Avatar answered Oct 05 '22 13:10

Sleiman Jneidi


You can use Math.addExact

stream.reduce(0, Math::addExact)
like image 38
Deadpool Avatar answered Oct 05 '22 13:10

Deadpool