How to implement DoubleToIntFunction using lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

DoubleToIntFunction is a functional interface defined in java.util.function package introduced in Java 8 version. This functional interface accepts a double-valued argument and produces an int-valued result. DoubleToIntFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().

Syntax

@FunctionalInterface
interface DoubleToIntFunction {
 int applyAsInt(double value)
}

Example

import java.util.function.DoubleToIntFunction;

public class DoubleToIntFunctionTest {
   public static void main(String args[]) {
      DoubleToIntFunction test = doubleVal -> {     // lambda expression
         int intVal = (int) doubleVal;
         return intVal;
      };
      double input = 50.99;
      System.out.println("input: " + input);
      int result = test.applyAsInt(input);
      System.out.println("Result: " + result);
   }
}

Output

input: 50.99
Result: 50
raja
Published on 24-Jan-2020 16:41:30
Advertisements