Java8 内置函数式接口

        //断言
        Predicate p = (s) -> s.startsWith("j");
        boolean result = p.test("java");
        System.out.println(result);

        //消费者
        Consumer consumer = System.out::println;
        consumer.accept("hello java8");

        //函数
        Function function = String::length;
        System.out.println(function.apply("lambda"));

        //有两个输入参数的函数
        BiFunction biFunction = (s, i) -> null != s && i > 0;
        System.out.println(biFunction.apply("hello", 19));

        //一元函数 (输入输出类型相同)
        UnaryOperator unaryOperator = (s) -> s += " lambda";
        System.out.println(unaryOperator.apply("hello"));

        // 二元函数(输入输出类型相同)
        BinaryOperator binaryOperator = (s, f) -> s + f;
        System.out.println(   binaryOperator.apply("easy ", " java"));

控制台输出

true
hello java8
6
true
hello lambda
easy  java

你可能感兴趣的:(Java8 内置函数式接口)