四大核心函数式接口
1. Supplier<T> : 供给型接口
描述:(参数为空返回值为T) T get();
//需求:产生指定个数的整数,并放入集合中
public List<Integer> getNumList(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n = sup.get();
list.add(n);
}
return list;
}
public void test2(){
List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
for (Integer num : numList) {
System.out.println(num);
}
}
2. Consumer<T> : 消费型接口
(参数为T返回值为空) void accept(T t);
3. Function<T, R> : 函数型接口
(参数为T返回值为R) R apply(T t);
4.Predicate<T> : 断言型接口
(参数为T返回值为boolean) boolean test(T t);