美文网首页
Java 函数式编程

Java 函数式编程

作者: bowen_wu | 来源:发表于2021-07-18 17:39 被阅读0次

    函数式编程

    优点:

    • 减少工作量
    • 提高效率
    • 减少 bug

    函数接口 @FunctionalInterface

    任何只包含一个抽象方法的接口都可以被自动转换为函数接口

    • lambda 表达式 => 短小精悍
    • 方法引用 => 使用一个类名 + 静态方法名把符合函数接口的静态方法自动的转化成函数接口 => Main::userWithEvenId => 有名字 + 可以更加复杂
      • static method => 静态方法
      • instance method => 实例方法

    lambda 一行,其余使用方法引用

    Java 8 函数接口

    1. java.util.function.Consumer<T> => void accept(T t); => Represent an operation that accepts a single input argument and returns no result. Unlike most other functional interface, Consumer is expected to operate via side-effects => Iterable.forEach(Consumer<? super T> action)

      users.forEach(user -> System.out.println(user)); // lambda
      users.forEach(System.out::println); // 方法引用
      
      • java.util.function.BiConsumer<T, U> => void accept(T t, U u);
      • java.util.function.DoubleConsumer => void accept(double value);
      • java.util.function.IntConsumer => void accept(int value);
      • java.util.function.LongConsumer => void accept(long value);
      • java.util.function.ObjDoubleConsumer<T> => void accept(T t, double value);
      • java.util.function.ObjIntConsumer<T> => void accept(T t, int value);
      • java.util.function.ObjLongConsumer<T> => void accept(T t, long value);
    2. java.util.function.Function<T, R> => R apply(T t); => Represents a function that accepts one argument and produces a result

      • java.util.function.BiFunction<T, U, R> => R apply(T t, U u);
      • java.util.function.DoubleFunction<R> => R apply(double value);
      • java.util.function.DoubleToIntFunction => int apply(double value);
      • java.util.function.DoubleToLongFunction => long apply(double value);
      • java.util.function.IntFunction<R> => R apply(int value);
      • java.util.function.IntToDoubleFunction => double applyAsDouble(int value);
      • java.util.function.IntToLongFunction => long applyAsLong(int value);
      • java.util.function.LongFunction<R> => R apply(long value);
      • java.util.function.LongToDoubleFunction => double applyAsDouble(long value);
      • java.util.function.LongToIntFunction => int applyAsInt(long value);
      • java.util.function.ToDoubleBiFunction<T, U> => double applyAsDouble(T t, U u);
      • java.util.function.ToDoubleFunction<T> => double applyAsDouble(T value);
      • java.util.function.ToIntBiFunction<T, U> => int applyAsInt(T t, U u);
      • java.util.function.ToIntFunction<T> => int applyAsInt<T value>
      • java.util.function.ToLongBiFunction<T, U> => long applyAsLong(T t, U u);
      • java.util.function.ToLongFunction<T> => long applyAsLong(T value);
    3. java.util.function.Predicate<T> => boolean test(T t); => Represents a predicate (boolean-valued function) of one argument. => Java 8 之前只能使用接口 + 匿名类的方法实现一个抽象式的操作,一个函数式的操作

      • java.util.function.BiPredicate<T, U> => boolean test(T t, U u);
      • java.util.function.DoublePredicate => boolean test(double value);
      • java.util.function.IntPredicate => boolean test(int value);
      • java.util.function.LongPredicate => boolean test(long value);
    4. java.util.function.Supplier<T> => T get() => Represents a supplier of results. => create(Object::new) => 方法引用 Object::new 可以转化为 Supplier

      • java.util.function.BooleanSupplier => boolean getAsBoolean();
      • java.util.function.DoubleSupplier => double getAsDouble();
      • java.util.function.IntSupplier => int getAsInt();
      • java.util.function.LongSupplier => long getAsLong();
    5. java.util.function.DoubleBinaryOperator => double applyAsDouble(double left, double right);

    6. java.util.function.DoubleUnaryOperator => double applyAsDouble(double operand);

    7. java.util.function.IntBinaryOperator => int applyAsInt(int left, int right);

    8. java.util.function.IntUnaryOperator => int applyAsInt(int operand);

    9. java.util.function.LongBinaryOperator => long applyAsLong(long left, long right);

    10. java.util.function.LongUnaryOperator => long applyAsLong(long operand);

    11. java.util.function.BinaryOperator<T> extends BiFunction<T, T, T>

    12. java.util.function.UnaryOperator<T> extends Function<T, T>

    java.util.Comparator<T>

    • compare() | reversed()
    • comparing() | thenComparing

    知识点:

    1. Google guava => 类库
    2. 实例方法的第一个参数都是 this,如果没有显示声明,JVM 会偷偷添加一个
    3. 自增的 Integer
      AtomicInteger i = new AtomicInteger(0);
      i.getAndIncrement(); // Atomically increments by one the current value
      
    4. SetComparator 需要在最后添加唯一标识
      TreeSet<Order> result = new TreeSet<>(
              comparing((Order order) -> order.isOpen() ? -1 : 1) // 这里可以优化为 comparing(Order::isOpen)
              .thenComparingInt(order -> -order.getAmount().intValue()) // 这里可以优化为 .then(Order::getAmount).reversed()
              .thenComparing(Order::getOrderTime)
              .thenComparing(Order::getId) // 注意这里
      );
      
      // 如果有两条数据只有 id 不同,其余都相同,如果没有 thenComparing(Order::getId) 则会丢失一条数据
      

    相关文章

      网友评论

          本文标题:Java 函数式编程

          本文链接:https://www.haomeiwen.com/subject/iqlapltx.html