Function 根据提供的值以及提供的函数返回新的值。Functional Method是apply。

    1. /**
    2. * Applies this function to the given argument.
    3. *
    4. * @param t the function argument
    5. * @return the function result
    6. */
    7. R apply(T t);

    compose方法用来结合两个Function函数,先运行的是before

    1. /**
    2. * Returns a composed function that first applies the {@code before}
    3. * function to its input, and then applies this function to the result.
    4. * If evaluation of either function throws an exception, it is relayed to
    5. * the caller of the composed function.
    6. *
    7. * @param <V> the type of input to the {@code before} function, and to the
    8. * composed function
    9. * @param before the function to apply before this function is applied
    10. * @return a composed function that first applies the {@code before}
    11. * function and then applies this function
    12. * @throws NullPointerException if before is null
    13. *
    14. * @see #andThen(Function)
    15. */
    16. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    17. Objects.requireNonNull(before);
    18. return (V v) -> apply(before.apply(v));
    19. }

    andThen方法与compose相反,先执行本函数在执行after函数

    1. /**
    2. * Returns a composed function that first applies this function to
    3. * its input, and then applies the {@code after} function to the result.
    4. * If evaluation of either function throws an exception, it is relayed to
    5. * the caller of the composed function.
    6. *
    7. * @param <V> the type of output of the {@code after} function, and of the
    8. * composed function
    9. * @param after the function to apply after this function is applied
    10. * @return a composed function that first applies this function and then
    11. * applies the {@code after} function
    12. * @throws NullPointerException if after is null
    13. *
    14. * @see #compose(Function)
    15. */
    16. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    17. Objects.requireNonNull(after);
    18. return (T t) -> after.apply(apply(t));
    19. }

    identity输出输入

    1. /**
    2. * Returns a function that always returns its input argument.
    3. *
    4. * @param <T> the type of the input and output objects to the function
    5. * @return a function that always returns its input argument
    6. */
    7. static <T> Function<T, T> identity() {
    8. return t -> t;
    9. }

    Function Fi在流中的应用:

    • Stream中的map、flatMap
    • Stream中map、flatMap的其他数据类型变种
    1. @Test
    2. public void functionFi () {
    3. Function<String,String> revertFunction = new Function<String, String>() {
    4. @Override
    5. public String apply (String o) {
    6. return StringUtils.reverse(o);
    7. }
    8. };
    9. List<String> strList = Arrays.asList("john", "xiaohundun", "sam", "joe", "gg");
    10. strList.stream().map(String::toUpperCase).map(revertFunction).forEach(System.out::println);
    11. Function<String,String> composeFunction = revertFunction.compose(new Function<Object, String>() {
    12. @Override
    13. public String apply (Object o) {
    14. return ((String) o).toUpperCase();
    15. }
    16. });
    17. System.out.println("##########");
    18. strList.stream().map(composeFunction).forEach(System.out::println);
    19. System.out.println("##########");
    20. strList.stream().map(Function.identity()).forEach(System.out::println);
    21. }

    虚区:

    1. NHOJ
    2. NUDNUHOAIX
    3. MAS
    4. EOJ
    5. GG
    6. ##########
    7. NHOJ
    8. NUDNUHOAIX
    9. MAS
    10. EOJ
    11. GG
    12. ##########
    13. john
    14. xiaohundun
    15. sam
    16. joe
    17. gg

    Function Fi在非流中的应用:

    • javafx的很多类中
    • Map中的computeIfAbsent
    • Optional中