Java.util.function.LongBinaryOperator interface with Examples

The LongBinaryOperator interface was introduced in Java 8. It represents an operation on two long values and returns the result as a long value. It is a functional interface and thus can be used as a lambda expression or in a method reference. It is mostly used when the operation needs to be encapsulated from the user.

Methods:

  1. applyAsLong(): This function takes two long values, performs the required operation and returns the result as a long.



    public long applyAsLong(long val1, long val2)
    

Example to demonstrate LongBinaryOperator interface as a lambda expression .

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate LongBinaryOperator
  
import java.util.function.LongBinaryOperator;
  
public class LongBinaryOperatorDemo {
    public static void main(String[] args)
    {
  
        LongBinaryOperator longBinaryOperator = (x, y) ->
        {
            return x + y;
        };
  
        System.out.print("343666 + 547477 = ");
        System.out.println(longBinaryOperator
                               .applyAsLong(343666, 547477));
  
        LongBinaryOperator longBinaryOperator1 = (x, y) ->
        {
            return x * y;
        };
        System.out.print("343666 * 547477 = ");
        System.out.println(longBinaryOperator1
                               .applyAsLong(343666, 547477));
    }
}

chevron_right


Output:

343666 + 547477 = 891143
343666 * 547477 = 188149230682

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/function/LongBinaryOperator.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.