如何在Java的lambda表达式中使用IntSupplier?
一个IntSupplier 是在定义的官能接口“java.util.function”包。此接口表示一个 不带参数的操作,并返回int 类型的结果。IntSupplier 接口只有一个方法getAsInt() 并返回结果。此功能接口可用作lambda 表达式 或方法 引用的分配目标。
语法
@FunctionalInterface
public interface IntSupplier {
int getAsInt();
}示例
import java.util.function.IntSupplier;
public class IntSupplierTest {
public static void main(String[] args) {
IntSupplier intSupplier1 = () -> Integer.MAX_VALUE; // lamba expression System.out.println("The maximum value of an Integer is: " + intSupplier1.getAsInt());
IntSupplier intSupplier2 = () -> Integer.MIN_VALUE;
System.out.println("The minimum value of an Integer is: " + intSupplier2.getAsInt());
int a = 10, b = 20;
IntSupplier intSupplier3 = () -> a*b;
System.out.println("The multiplication of a and b is: " + intSupplier3.getAsInt());
}
}输出结果
The maximum value of an Integer is: 2147483647 The minimum value of an Integer is: -2147483648 The multiplication of a and b is: 200
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短