如何使用 lambda 表达式在 Java 中实现 DoubleToIntFunction?
DoubleToIntFunction 是 java.util.function 包中定义的一个函数接口,该包在 Java 8 版本中引入。此函数接口接受一个double 值参数,生成一个int 值结果。DoubleToIntFunction 接口可作为lambda 表达式或方法 引用的赋值目标。它仅包含一个抽象方法:applyAsInt()。
语法
@FunctionalInterface
interface DoubleToIntFunction {
int applyAsInt(double value)
}实例
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);
}
}输出
input: 50.99 Result: 50
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP