LongSupplier 是来自 java.util.function 包的内置函数接口。该接口不需要任何输入,但生成Long值输出。因为 LongSupplier 是一个函数接口,所以它可以用作 lambda 表达式和方法引用的赋值目标,并且只包含一个抽象方法: getAsLong ()。
@FunctionalInterface
public interface LongSupplier {
long getAsLong();
}import java.util.function.LongSupplier;
public class LongSupplierLambdaTest {
public static void main(String args[]) {
LongSupplier supplier = () -> { // lambda 表达式
return 75;
};
long result = supplier.getAsLong();
System.out.println(result);
}
}输出结果
75
import java.util.function.LongSupplier;
public class LongSupplierMethodRefTest {
public static void main(String[] args) {
LongSupplier supplier = LongSupplierMethodRefTest::getValue; //方法引用
double result = supplier.getAsLong();
System.out.println(result);
}
static long getValue() {
return 50;
}
}输出结果
50.0