💡 函数式编程

一、 Function 源码:

  1. /*
  2. * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package java.util.function;
  6. import java.util.Objects;
  7. /**
  8. * Represents a function that accepts one argument and produces a result.
  9. *
  10. * <p>This is a <a href="package-summary.html">functional interface</a>
  11. * whose functional method is {@link #apply(Object)}.
  12. *
  13. * @param <T> the type of the input to the function
  14. * @param <R> the type of the result of the function
  15. *
  16. * @since 1.8
  17. */
  18. @FunctionalInterface
  19. public interface Function<T, R> {
  20. /**
  21. * Applies this function to the given argument.
  22. *
  23. * @param t the function argument
  24. * @return the function result
  25. */
  26. R apply(T t);
  27. /**
  28. * Returns a composed function that first applies the {@code before}
  29. * function to its input, and then applies this function to the result.
  30. * If evaluation of either function throws an exception, it is relayed to
  31. * the caller of the composed function.
  32. *
  33. * @param <V> the type of input to the {@code before} function, and to the
  34. * composed function
  35. * @param before the function to apply before this function is applied
  36. * @return a composed function that first applies the {@code before}
  37. * function and then applies this function
  38. * @throws NullPointerException if before is null
  39. *
  40. * @see #andThen(Function)
  41. */
  42. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  43. Objects.requireNonNull(before);
  44. return (V v) -> apply(before.apply(v));
  45. }
  46. /**
  47. * Returns a composed function that first applies this function to
  48. * its input, and then applies the {@code after} function to the result.
  49. * If evaluation of either function throws an exception, it is relayed to
  50. * the caller of the composed function.
  51. *
  52. * @param <V> the type of output of the {@code after} function, and of the
  53. * composed function
  54. * @param after the function to apply after this function is applied
  55. * @return a composed function that first applies this function and then
  56. * applies the {@code after} function
  57. * @throws NullPointerException if after is null
  58. *
  59. * @see #compose(Function)
  60. */
  61. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  62. Objects.requireNonNull(after);
  63. return (T t) -> after.apply(apply(t));
  64. }
  65. /**
  66. * Returns a function that always returns its input argument.
  67. *
  68. * @param <T> the type of the input and output objects to the function
  69. * @return a function that always returns its input argument
  70. */
  71. static <T> Function<T, T> identity() {
  72. return t -> t;
  73. }
  74. }

1.1 )apply(T t);方法:

翻译过来就是:将给定的参数应用到这个函数上,传入的参数类型为T返回类型为R

  1. public class TestFunction {
  2. public static void main(String[] args) {
  3. System.out.println(testFunction(10, integer -> integer * 2));
  4. }
  5. public static int testFunction(int i, Function<Integer, Integer> function) {
  6. return function.apply(i);
  7. }
  8. }
  9. -- 输出:20

1.2) compose(Function<? super V, ? extends T> before)

翻译过来就是:接收一个function类型的函数作为参数

  1. public class TestFunctionofcompose {
  2. public static void main(String[] args) {
  3. System.out.println(testFunction(10, i -> i * 2 + 9, j -> j * j));
  4. }
  5. public static int testFunction(int i, Function<Integer, Integer> function1, Function<Integer, Integer> function2 ) {
  6. return function1.compose(function2).apply(i);
  7. }
  8. }
  9. -- 输出: 209

1.3) andThen(Function<? super R, ? extends V> after)
翻译过来就是:就是现将传过来的参数执行apply(T t)方法,之后把apply(T t)里面返回的结果再去执行第二个Function函数

https://blog.csdn.net/qq_27416233/article/details/84832013