java8 函数接口例子

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

猜你喜欢

转载自blog.csdn.net/weixin_43996899/article/details/91986270