How to implement LongFunction using lambda and method reference in Java?

LongFunction<R> is an in-built functional interface defined in java.util.function package. This functional interface expects a long-valued parameter as input and produces a result. LongFunction<R> interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: apply().


public interface LongFunction<R> {
 R apply(long value)


import java.util.function.LongFunction;

public class LongFunctionTest {
   public static void main(String[] args) {
      LongFunction<Long> function1 = (long i) -> { // lambda expression
         return i + i;
      System.out.println("Using Lambda Expression: " + function1.apply(10));
      LongFunction<Long> function2 = LongFunctionTest::add; // method reference
      System.out.println("Usimg Method Reference: " + function2.apply(20));
   static long add(long i) {
      return i + i;


Using Lambda Expression: 20
Usimg Method Reference: 40