java8 函数式编程

函数式接口

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。 函数式接口可以被隐式转换为 lambda 表达式。 Lambda 表达式和方法引用(实际上也可认为是Lambda表达式)上。

  • 定义函数式接口
@FunctionalInterface
interface Hello {
  void helloWorld(String name);
}
  • 实现该接口
Hello hello = System.out::println;
hello.helloWorld("Hello World!!!");

JDK 1.8 新增的函数接口

java.util.function 包含了很多类,用来支持Java的函数式编程.

1. Operator 接口
  1. BinaryOperator<T>代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果
  2. DoubleBinaryOperator 接受两个参数同为类型double,返回值类型也为double
  3. IntBinaryOperator 接受两个参数同为类型int,返回值类型也为int
  4. LongBinaryOperator 接受两个参数同为类型long,返回值类型也为long
  5. DoubleUnaryOperator 接受一个参数同为类型double,返回值类型也为double
  6. IntUnaryOperator 接受一个参数同为类型int,返回值类型也为int
  7. LongUnaryOperator 接受一个参数同为类型long,返回值类型也为long
  8. UnaryOperator<T> 接受一个参数为类型T,返回值类型也为T
  • 代码栗子
    // BinaryOperator 继承了 BiFunction, 可执行 apply 方法
    BinaryOperator<Integer> binaryOperator = (num1, num2) -> num1 * num1;
    System.out.println("俩数之和:" + binaryOperator.apply(1, 2));
    
    // BinaryOperator 有两个静态方法,是用于比较两个元素的大小
    BinaryOperator<Integer> minBy = BinaryOperator.minBy(Comparator.naturalOrder());
    BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(Comparator.naturalOrder());
    System.out.println("较小元素:" + minBy.apply(1, 2) + " , 较大元素:" + maxBy.apply(1, 2));
    
    // 求俩Double的和
    DoubleBinaryOperator d = Double::sum; 
    System.out.println("Double: "+ d.applyAsDouble(1.1, 2.2));
    
    
    
2. Consumer 接口
  1. Consumer<T> 接受一个输入参数并且无返回的操作
  2. BiConsumer<T,U> 接受两个输入参数的操作,并且不返回任何结果
  3. DoubleConsumer 接受double值参数的操作,并且不返回结果
  4. IntConsumer 接受一个int类型的输入参数,无返回值
  5. LongConsumer 接受一个long类型的输入参数,无返回值
  6. ObjDoubleConsumer<T> 接受一个object类型和一个double类型的输入参数,无返回值
  7. ObjIntConsumer<T> 接受一个object类型和一个int类型的输入参数,无返回值
  8. ObjLongConsumer<T> 接受一个object类型和一个long类型的输入参数,无返回值
  • 代码栗子
    Consumer<String> consumer = name -> System.out.println("Hello " + name);
    consumer.accept("World");
    
    BiConsumer<Integer, Integer> biConsumer = (num1, num2) -> System.out.println("俩数之和:"+num1 * num2);
    biConsumer.accept(1,2);
    
3. Function 接口
  1. Function<T,R> 接受一个输入参数,返回一个结果
  2. BiFunction<T,U,R> 接受两个输入参数的方法,并且返回一个结果
  3. DoubleFunction<R> 接受一个double值参数的方法,并且返回结果
  4. DoubleToIntFunction 接受一个double类型输入,返回一个int类型结果
  5. DoubleToLongFunction 接受一个double类型输入,返回一个long类型结果
  6. IntFunction<R> 接受一个int类型输入参数,返回一个结果
  7. IntToDoubleFunction 接受一个int类型输入,返回一个double类型结果
  8. IntToLongFunction 接受一个int类型输入,返回一个long类型结果
  9. LongFunction<R> 接受一个long类型输入参数,返回一个结果
  10. LongToDoubleFunction 接受一个long类型输入,返回一个int类型结果
  11. LongToIntFunction 接受一个long类型输入,返回一个int类型结果
  12. ToDoubleBiFunction<T,U> 接受两个输入参数,返回一个double类型结果
  13. ToIntBiFunction<T,U> 接受两个输入参数,返回一个int类型结果
  14. ToLongBiFunction<T,U> 接受两个输入参数,返回一个long类型结果
  15. ToDoubleFunction<T> 接受一个输入参数,返回一个double类型结果
  16. ToIntFunction<T> 接受一个输入参数,返回一个int类型结果
  17. ToLongFunction<T> 接受一个输入参数,返回一个long类型结果
  • 代码栗子
    BiFunction<Integer, Integer, Integer> biFunction = (num1, num2) -> num1 * num2;
    System.out.println("俩数之和:" + biFunction.apply(1,2));
    
4. Predicate 接口
  1. Predicate<T> 接受一个输入参数,返回一个布尔值结果
  2. BiPredicate<T,U> 代表了一个两个参数的boolean值方法
  3. DoublePredicate 接受一个double输入参数,返回一个布尔值的结果
  4. IntPredicate 接受一个int输入参数,返回一个布尔值的结果
  5. LongPredicate 接受一个long输入参数,返回一个布尔值类型结果
  • 代码栗子
    Predicate<Integer> predicate = p -> p > 10;
    System.out.println("1 大于 10 " +predicate.test(1));
    
5. Supplier 接口
  1. Supplier<T> 无参数,返回一个结果
  2. BooleanSupplier 代表了boolean值结果的提供方
  3. DoubleSupplier 代表一个double值结构的提供方
  4. IntSupplier 无参数,返回一个int类型结果
  5. LongSupplier 无参数,返回一个结果long类型的值
  • 代码栗子
    Supplier supplier = () -> 10;
    System.out.println("结果: "+supplier.get());
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值