JDK8 - 四大函数式接口

java内置的4大核心函数式接口:

消费型接口 Consumer<T>       void accept(T t)

供给型接口 Supplier<T>           T get()

函数型接口 Function<T, R>      R apply(T t)

断定型接口 Predicate<T>        boolean test(T t) 

 

对应的demo:

public class FunctionInterfaceTest {
    public static void main(String[] args) {
        Consumer<String> consumer = str -> System.out.println(str);
        consumer.accept("hello");

        Supplier<String> supplier = () -> "hi";
        System.out.println(supplier.get());

        Function<Integer, String> function = num -> "str" + num;
        System.out.println(function.apply(12));

        Predicate<Integer> predicate = num -> num > 0 ? true : false;
        System.out.println(predicate.test(12));
    }
}

 

posted @ 2021-06-15 21:23  圣金巫灵  阅读(222)  评论(0编辑  收藏  举报