Java8 中的常用函数式接口

总结一些java 8中常用的函数式接口

  1. 函数式接口: Predicate < T >
    函数描述符: T ==> boolean
    原始类型特化:IntPredicate, LongPredicate, DoublePredicate
      // 判断是否是正数
       IntPredicate predicate = i -> (i > 0);
    
       // true  
       predicate.test(5);
    
  2. 函数式接口: Consumer < T >
    函数描述符: T ==> void
    原始类型特化:IntConsumer,LongConsumer, DoubleConsumer
       // 输出数字到控制台
       IntConsumer consumer = i -> System.out.println(i);
       //IntConsumer consumer = System.out::println;
       
       // 5 
       consumer.accept(5);
    
  3. 函数式接口: Function <T , R>
    函数描述符: T ==> R
    原始类型特化:
    IntFunction < R >, IntToDoubleFunction, IntToLongFunction,
    LongFunction < R >, LongToDoubleFunction, LongToIntFunction,
    DoubleFunction < R >,
    ToIntFunction < T >, ToDoubleFunction < T >, ToLongFunction < T >
       // 整型转字符串
       IntFunction<String> int2StringFunc = i -> (i + "");
       
       //"10" 
       int2StringFunc.apply(10);
    
       // 字符串转整型
       ToIntFunction<String> string2IntFunc = s -> Integer.parseInt(s);
       //ToIntFunction<String> string2IntFunc = Integer::parseInt;
       
       // 22  
       string2IntFunc.applyAsInt("22");
    
  4. 函数式接口: Supplier < T >
    函数描述符: () ==> T
    原始类型特化:BooleanSupplier,IntSupplier, LongSupplier, DoubleSupplier
       // 无中生有
       Supplier<String> supplier = () -> "Welcome to my blog";
       
       // "Welcome to my blog"  
       supplier.get();
    
  5. 函数式接口: UnaryOperator < T >
    函数描述符: T ==> T
    原始类型特化:IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator
       // 平方
       IntUnaryOperator operator = i -> i * i;
    
       // 25 
       operator.applyAsInt(5);
    
  6. 函数式接口: BinaryOperator< T >
    函数描述符: (T, T) ==> T
    原始类型特化:IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator
       // 两数之和
       IntBinaryOperator sum = (i, j) -> i + j;
    
       // 11  
       sum.applyAsInt(5, 6);
    
  7. 函数式接口: BiPredicate < L, R >
    函数描述符: (L, R) ==> boolean
    原始类型特化:无
       // 判断数值是否相等
       BiPredicate<Integer, String> biPredicate = (i, s) -> i == Integer.parseInt(s);
    
       // true 
       biPredicate.test(5, "5");
       
       // false
       biPredicate.test(5, "4");
    
  8. 函数式接口: BiConsumer < T, U >
    函数描述符: (T, U) ==> void
    原始类型特化:ObjIntConsumer < T >, ObjLongConsumer < T >, ObjDoubleConsumer < T >
       String format = "%s is %d years old";
       BiConsumer<String, Integer> biConsumer = (s, i) -> {
           String result = String.format(format, s, i);
           System.out.println(result);
       };
    
       // Tom is 15 years old
       biConsumer.accept("Tom", 15);
    
  9. 函数式接口: BiFunction < T, U, R >
    函数描述符: (T, U) ==> R
    原始类型特化:ToIntBiFunction < T, U >, ToDoubleBiFunction < T, U >, ToLongBiFunction < T, U >
       // 加入HashMap中
       BiFunction<String, Integer, Map> biFunction = (s, i) -> {
           Map<String, Integer> map = new HashMap<>();
           map.put(s, i);
           return map;
       };
    
       // {"key" : 100}
       biFunction.apply("key", 100);
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值