如何使用 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