java 8提供的4个最核心函数式接口,写的非常简单一看就懂
函数式接口使用 @FunctionalInterface 申明该接口为函数式接口
1.Function (功能型接口)
interface Function
Function<String ,Boolean> function = "hello" :: startsWith;
System.out.println(function.apply("h"));2.Consumer (消费型接口)
interface Consumer
void accept(T t);
该接口接受一个参数但没有返回
例子:
Consumer<String> consumer = System.out :: print;
consumer.accept("abc");3.Supplier (供给型接口)
public interface Supplier
T get();
该接口只有返回值没有入参数
例子:
Supplier<String> supplier = "abncds" :: toUpperCase;
System.out.println(supplier.get());4.Predicate (断言型接口)
该接口接受一个输入参数,返回一个布尔值结果
interface Predicate
boolean test(T t);
例子:
Predicate<String> predicate = "sasd" ::equalsIgnoreCase;
System.out.println(predicate.test("SAsd"));版权声明:本文为weixin_37943875原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。