1 @FunctionalInterface

用于函数式接口类型声明的信息注解类型,接口实例被Lamada表示。方法应用,或者构造器应用创建。

1.1 要求

  • 函数式接口只能有一个抽象方法。
  • 排除接口默认方法
  • 不能覆盖实现Object的公开方法

接口满足函数式接口的要求,无论是否标注@FunctionalInterface,均能被编译器视作函数式接口。

1.2 接口类型

1.2.1 Supplier
  1. @FunctionalInterface
  2. public interface Supplier<T> {
  3. /**
  4. * Gets a result.
  5. *
  6. * @return a result
  7. */
  8. T get();
  9. }

1.2.2 Consumer
  1. @FunctionalInterface
  2. public interface Consumer<T> {
  3. /**
  4. * Performs this operation on the given argument.
  5. *
  6. * @param t the input argument
  7. */
  8. void accept(T t);
  9. /**
  10. * Returns a composed {@code Consumer} that performs, in sequence, this
  11. * operation followed by the {@code after} operation. If performing either
  12. * operation throws an exception, it is relayed to the caller of the
  13. * composed operation. If performing this operation throws an exception,
  14. * the {@code after} operation will not be performed.
  15. *
  16. * @param after the operation to perform after this operation
  17. * @return a composed {@code Consumer} that performs in sequence this
  18. * operation followed by the {@code after} operation
  19. * @throws NullPointerException if {@code after} is null
  20. */
  21. default Consumer<T> andThen(Consumer<? super T> after) {
  22. Objects.requireNonNull(after);
  23. return (T t) -> { accept(t); after.accept(t); };
  24. }
  25. }

1.2.3 Function

转换接口

  1. @FunctionalInterface
  2. public interface Function<T, R> {
  3. /**
  4. * Applies this function to the given argument.
  5. *
  6. * @param t the function argument
  7. * @return the function result
  8. */
  9. R apply(T t);
  10. /**
  11. * Returns a composed function that first applies the {@code before}
  12. * function to its input, and then applies this function to the result.
  13. * If evaluation of either function throws an exception, it is relayed to
  14. * the caller of the composed function.
  15. *
  16. * @param <V> the type of input to the {@code before} function, and to the
  17. * composed function
  18. * @param before the function to apply before this function is applied
  19. * @return a composed function that first applies the {@code before}
  20. * function and then applies this function
  21. * @throws NullPointerException if before is null
  22. *
  23. * @see #andThen(Function)
  24. */
  25. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  26. Objects.requireNonNull(before);
  27. return (V v) -> apply(before.apply(v));
  28. }
  29. /**
  30. * Returns a composed function that first applies this function to
  31. * its input, and then applies the {@code after} function to the result.
  32. * If evaluation of either function throws an exception, it is relayed to
  33. * the caller of the composed function.
  34. *
  35. * @param <V> the type of output of the {@code after} function, and of the
  36. * composed function
  37. * @param after the function to apply after this function is applied
  38. * @return a composed function that first applies this function and then
  39. * applies the {@code after} function
  40. * @throws NullPointerException if after is null
  41. *
  42. * @see #compose(Function)
  43. */
  44. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  45. Objects.requireNonNull(after);
  46. return (T t) -> after.apply(apply(t));
  47. }
  48. /**
  49. * Returns a function that always returns its input argument.
  50. *
  51. * @param <T> the type of the input and output objects to the function
  52. * @return a function that always returns its input argument
  53. */
  54. static <T> Function<T, T> identity() {
  55. return t -> t;
  56. }
  57. }

1.2.4 Predicate
  1. @FunctionalInterface
  2. public interface Predicate<T> {
  3. /**
  4. * Evaluates this predicate on the given argument.
  5. *
  6. * @param t the input argument
  7. * @return {@code true} if the input argument matches the predicate,
  8. * otherwise {@code false}
  9. */
  10. boolean test(T t);
  11. /**
  12. * Returns a composed predicate that represents a short-circuiting logical
  13. * AND of this predicate and another. When evaluating the composed
  14. * predicate, if this predicate is {@code false}, then the {@code other}
  15. * predicate is not evaluated.
  16. *
  17. * <p>Any exceptions thrown during evaluation of either predicate are relayed
  18. * to the caller; if evaluation of this predicate throws an exception, the
  19. * {@code other} predicate will not be evaluated.
  20. *
  21. * @param other a predicate that will be logically-ANDed with this
  22. * predicate
  23. * @return a composed predicate that represents the short-circuiting logical
  24. * AND of this predicate and the {@code other} predicate
  25. * @throws NullPointerException if other is null
  26. */
  27. default Predicate<T> and(Predicate<? super T> other) {
  28. Objects.requireNonNull(other);
  29. return (t) -> test(t) && other.test(t);
  30. }
  31. /**
  32. * Returns a predicate that represents the logical negation of this
  33. * predicate.
  34. *
  35. * @return a predicate that represents the logical negation of this
  36. * predicate
  37. */
  38. default Predicate<T> negate() {
  39. return (t) -> !test(t);
  40. }
  41. /**
  42. * Returns a composed predicate that represents a short-circuiting logical
  43. * OR of this predicate and another. When evaluating the composed
  44. * predicate, if this predicate is {@code true}, then the {@code other}
  45. * predicate is not evaluated.
  46. *
  47. * <p>Any exceptions thrown during evaluation of either predicate are relayed
  48. * to the caller; if evaluation of this predicate throws an exception, the
  49. * {@code other} predicate will not be evaluated.
  50. *
  51. * @param other a predicate that will be logically-ORed with this
  52. * predicate
  53. * @return a composed predicate that represents the short-circuiting logical
  54. * OR of this predicate and the {@code other} predicate
  55. * @throws NullPointerException if other is null
  56. */
  57. default Predicate<T> or(Predicate<? super T> other) {
  58. Objects.requireNonNull(other);
  59. return (t) -> test(t) || other.test(t);
  60. }
  61. /**
  62. * Returns a predicate that tests if two arguments are equal according
  63. * to {@link Objects#equals(Object, Object)}.
  64. *
  65. * @param <T> the type of arguments to the predicate
  66. * @param targetRef the object reference with which to compare for equality,
  67. * which may be {@code null}
  68. * @return a predicate that tests if two arguments are equal according
  69. * to {@link Objects#equals(Object, Object)}
  70. */
  71. static <T> Predicate<T> isEqual(Object targetRef) {
  72. return (null == targetRef)
  73. ? Objects::isNull
  74. : object -> targetRef.equals(object);
  75. }
  76. }

1.2.5 Runnable
  1. @FunctionalInterface
  2. public interface Runnable {
  3. /**
  4. * When an object implementing interface <code>Runnable</code> is used
  5. * to create a thread, starting the thread causes the object's
  6. * <code>run</code> method to be called in that separately executing
  7. * thread.
  8. * <p>
  9. * The general contract of the method <code>run</code> is that it may
  10. * take any action whatsoever.
  11. *
  12. * @see java.lang.Thread#run()
  13. */
  14. public abstract void run();
  15. }

1.3 原理

MethodHandle
InvokeDynamic

2 函数式接口设计

2.1 Supplier

  • 基本特点:只出不进
  • 编程范式:作为方法/构造参数,方法返回值
  • 使用场景:数据来源,代码替代接口。
  • 供应,加工数据。

    1. () -> {return data};

    2.2 Consumer
  • 基本特点:只进不出

  • 编程方式:作为犯法/构造参数
  • 使用场景:执行Callback
  • 消费,输出数据

    2.3 Function
  • 基本特点:有进有出

  • 编程范式:作为方法/构造参数
  • 使用场景:类型转换,业务处理

    1. Function<Number,Number> muility = (a) = a* a;
    2. Function<String, Long> stringToLong = Long::valueOf;

    2.4 Predicate
  • 基本特点:boolean类型判断。

  • 编程范式:作为方法/构造参数
  • 使用场景:过滤,对象比较等。
    1. num -> num %2 !=0;

    3 Stream API 设计

    3.1 转换Stream#map
    3.2 过滤Stream#filter
    3.3 排序Stream#Sorted
    Collect