美文网首页
Function、Predicate、Consumer、Supp

Function、Predicate、Consumer、Supp

作者: 炮灰向前冲啦 | 来源:发表于2018-03-06 18:01 被阅读0次

这些接口都有一个@FunctionalInterface注解,表明这个接口将是一个函数式接口,里面只能有一个抽象方法

Function

Function<T, R> => R apply(T t);
接受一个输入参数,返回一个结果

Function<Integer, String> function1 = (x) -> "result: " + x;
function1.apply(6);

Predicate

Predicate<T> => boolean test(T t);
接受一个输入参数,返回布尔值

Predicate<String> predicate = (x) -> x.length() > 0;
predicate.test("String");

Consumer

Consumer<T> => void accept(T t);
接受一个输入参数,无返回值

Consumer<String> consumer = (x) -> System.out.println("consumer: " + x);
consumer.accept("Hello");

Supplier

Supplier<T> => T get();
无输入参数,返回一个结果

Supplier<String> supplier = () -> "Test supplier";
supplier.get();

相关文章

网友评论

      本文标题:Function、Predicate、Consumer、Supp

      本文链接:https://www.haomeiwen.com/subject/djyrfftx.html