IntToDoubleFunction 是java.util.function包中的功能接口。该功能接口接受一个int值的参数并产生一个double值的结果。IntToDoubleFunction可以用作lambda 表达式 或方法引用的分配目标。它仅包含一个抽象方法:applyAsDouble()。
@FunctionalInterface interface IntToDoubleFunction { double applyAsDouble(int value); }
import java.util.function.IntToDoubleFunction;; public class IntToDoubleLambdaTest { public static void main(String[] args) { IntToDoubleFunction getDouble = intVal -> { //lambda表达式 double doubleVal = intVal; return doubleVal; }; int input = 25; double result = getDouble.applyAsDouble(input); System.out.println("double值是: " + result); input = 50; System.out.println("double值是: " + getDouble.applyAsDouble(input)); input = 75; System.out.println("double值是: " + getDouble.applyAsDouble(input)); } }
输出结果
double值是: 25.0 double值是: 50.0 double值是: 75.0
import java.util.function.IntToDoubleFunction; public class IntToDoubleMethodRefTest { public static void main(String[] args) { IntToDoubleFunction result = IntToDoubleMethodRefTest::convertIntToDouble; //方法引用 System.out.println(result.applyAsDouble(50)); System.out.println(result.applyAsDouble(100)); } static Double convertIntToDouble(int value) { return value / 10d; } }
输出结果
5.0 10.0