java8函数式编程内置函数小结

java8函数式编程 Function


        // Function<T, R> -T作为输入,返回的R作为输出 
        Function<String,String> function = (x) -> {System.out.print(x+": ");return "Function";};
        System.out.println(function.apply("hello world"));
        
        //Predicate<T> -T作为输入,返回的boolean值作为输出 
        Predicate<String> pre = (x) ->{System.out.print(x);return false;};
        System.out.println(": "+pre.test("hello World"));
        
        //Consumer<T> - T作为输入,执行某种动作但没有返回值 
        Consumer<String> con = (x) -> {System.out.println(x);};
        con.accept("hello world");
        
        //Supplier<T> - 没有任何输入,返回T 
        Supplier<String> supp = () -> {return "Supplier";};
        System.out.println(supp.get());
        
        //BinaryOperator<T> -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用 
        BinaryOperator<String> bina = (x,y) ->{System.out.print(x+" "+y);return "BinaryOperator";};
        System.out.println("  "+bina.apply("hello ","world"));
// 输出
hello world: Function
hello World: false
hello world
Supplier
hello  world  BinaryOperator

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容