1、Consumer<T>消费型接口
/**
* 接口:Consumer<T>
* 内含抽象方法:void accept(T t)
* 接口类型:消费型接口
* 参数和返回类型:有参(T为参数类型),无返回值
* */
public static void main(String[] args) {
Consumer<Integer> consumer=(money)->System.out.println("今天去吃饭,吃了"+money+"元");
show(consumer,200);
}
private static void show(Consumer<Integer> consumer,Integer money) {
consumer. Accept(money);
}
2、Supplier<T>生产型/供给型接口
/**
* 接口:Supplier<T>
* 内含抽象方法:T get();
* 接口类型:生产型/供给型接口
* 参数和返回类型:无参,有返回值(T为返回类型)
* */
public static void main(String[] args) {
Supplier<Integer> supplier=()-> new Random().nextInt(10);
fun(supplier);
}
private static void fun(Supplier<Integer> supplier) {
Integer integer = supplier.get();
System.out.println("随机数字是:"+integer);
}
3、Function<T, R>函数型接口
/**
* 接口:Function<T, R>
* 内含抽象方法:R apply(T t);
* 接口类型:函数型接口
* 参数和返回类型:有参,有返回值(T为参数类型,R为返回类型)
* */
public static void main(String[] args) {
Function<String,String> function=str->str.toUpperCase();
fun(function,"hello word");
}
private static void fun(Function<String,String> function,String str) {
String apply = function.apply(str);
System.out.println(apply);
}
4、Predicate<T>断言型接口
/**
* 接口:Predicate<T>
* 内含抽象方法:boolean test(T t);
* 接口类型:断言型接口
* 参数和返回类型:有参,有返回值(T为参数类型,boolean为返回类型)
* */
public static void main(String[] args) {
Predicate<Integer> predicate=(age)-> age<18;
fun(predicate,17);
}
private static void fun(Predicate<Integer> predicate,int age) {
boolean test = predicate.test(age);
System.out.println("是否是未成年:"+test);
}