函数式编程
优点:
- 减少工作量
- 提高效率
- 减少 bug
函数接口 @FunctionalInterface
任何只包含一个抽象方法的接口都可以被自动转换为函数接口
- lambda 表达式 => 短小精悍
- 方法引用 => 使用一个类名 + 静态方法名把符合函数接口的静态方法自动的转化成函数接口 =>
Main::userWithEvenId
=> 有名字 + 可以更加复杂-
static method
=> 静态方法 -
instance method
=> 实例方法
-
lambda 一行,其余使用方法引用
Java 8 函数接口
-
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);
- java.util.function.BiConsumer<T, U> =>
-
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);
- java.util.function.BiFunction<T, U, R> =>
-
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);
- java.util.function.BiPredicate<T, U> =>
-
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();
- java.util.function.BooleanSupplier =>
-
java.util.function.DoubleBinaryOperator =>
double applyAsDouble(double left, double right);
-
java.util.function.DoubleUnaryOperator =>
double applyAsDouble(double operand);
-
java.util.function.IntBinaryOperator =>
int applyAsInt(int left, int right);
-
java.util.function.IntUnaryOperator =>
int applyAsInt(int operand);
-
java.util.function.LongBinaryOperator =>
long applyAsLong(long left, long right);
-
java.util.function.LongUnaryOperator =>
long applyAsLong(long operand);
-
java.util.function.BinaryOperator<T> extends BiFunction<T, T, T>
-
java.util.function.UnaryOperator<T> extends Function<T, T>
java.util.Comparator<T>
-
compare()
|reversed()
-
comparing()
|thenComparing
知识点:
- Google guava => 类库
- 实例方法的第一个参数都是
this
,如果没有显示声明,JVM 会偷偷添加一个 - 自增的
Integer
AtomicInteger i = new AtomicInteger(0); i.getAndIncrement(); // Atomically increments by one the current value
-
Set
的Comparator
需要在最后添加唯一标识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) 则会丢失一条数据
网友评论