How to implement DoubleToLongFunction using lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

DoubleToLongFunction is a built-in functional interface from java.util.function package introduced in Java 8. This functional interface accepts a double-valued parameter and produces a long-valued result. DoubleToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().

Syntax

@FunctionalInterface
public interface DoubleToLongFunction {
 long applyAsLong(double value)
}

Example

import java.util.function.DoubleToLongFunction;
public class DoubleToLongFunctionTest {
   public static void main(String args[]) {
      double dbl = 30.1212;
      DoubleToLongFunction castToLong = (dblValue) -> (long) dblValue; // lambda expression
      System.out.println(castToLong.applyAsLong(dbl));
      dbl = 77.9212;
      DoubleToLongFunction roundToLong = Math::round;
      System.out.println(roundToLong.applyAsLong(dbl));
   }
}

Output

30
78
raja
Published on 24-Jan-2020 12:51:39
Advertisements