java8为我们在不同场景下更灵活的使用lambda表达式,为我们预制了四大核心接口。
接口特征
- Consumer<T>:消费者接口,无返回值。
- Supplier<T>:生产者接口,有返回值。
- Function<T,R>:函数式接口,有入参有返回值。
- Predicate<T,R>:断言式接口,返回值为boolean。
1、Consumer<T>:消费性接口
void accept(T t)
/**
* 消费性接口:无返回值
*/
@Test
public void testConsumer(){
happy(1000D ,m-> System.out.println("锻炼身体,花费"+m));
}
public void happy(Double money,Consumer<Double> consumer){
consumer.accept(money);
}
2、Supplier<T>:共给性接口
T get();
@Test
public void testSupplier(){
List<Integer> list = getList(10, () -> (int) (Math.random() * 100));
for (Integer integer : list) {
System.out.println(integer