Consumer<T> 入参T,出参void Supplier<R> 入参void 返回R Predicate<T> 入参T,返回boolean function<T,R> 入参T,返回R 一下代码:
import java.util.function.*;
public class lambdaFirstlearn {
public static void ConmuserTest(Apple apple,Consumer<Apple> appleConsumer){
// 将apple修改
System.out.println("orgin apple:"+apple);
appleConsumer.accept(apple);
System.out.println("new apple"+apple);
}
public static void SupplierTest(Supplier<Apple> supplierget){
Apple apple = supplierget.get();
System.out.println(apple);
}
// test return boolean methond
public static void PredicateTest(Apple apple,Predicate<Apple> applePredicate){
boolean test = applePredicate.test(apple);
System.out.println(test);
}
// test function
public static void FunctionTest(Apple apple , Function<Apple,String> functionApple){
String apply = functionApple.apply(apple);
System.out.println(apply);
}
public static void main(String[] args) {
// test Consumer
Apple apple=new Apple("green",120);
ConmuserTest(apple,a->a.setColor(a.getColor()+"colordesc"));
System.out.println(apple);
// test supplier
System.out.println("-------------------------------");
SupplierTest(()->new Apple("blue",120));
System.out.println("-------------------------------");
// test predict
PredicateTest(apple,a->a.getColor().equals("green"));
// test function
FunctionTest(apple,a->a.getColor());
IntConsumer a;
BiConsumer b;
}
}
总结:主要更具接口定义java8给出的一些入参和出参不同的普遍接口,方便直接使用lambda使用的,但是考虑到泛型不能接受基本类型,所以还添加了一些像入参为int的IntConsumer,IntFunction,Intpredict,IntSupplier(出参),入参为Double,long的一些.........和一些入参为两个类型的Bifunction,BiPredict.......,这些都在util包中,使用方法类似4种基本的类型接口。