JDK中Funtion接口源码解析和使用详解
Posted 秋9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK中Funtion接口源码解析和使用详解相关的知识,希望对你有一定的参考价值。
目录
Functional Interface(功能接口)为lambda表达式和方法引用(用冒号::来进行方法的调用)提供目标类型。每个功能接口都有一个抽象方法,称为该功能接口的功能方法,lambda表达式的参数和返回类型与之匹配或适配。
Java8为函数式接口引入了一个新注解@FunctionalInterface
,主要用于编译级错误检查,加上该注解,当接口不符合函数式接口定义的时候,编译器会报错。
此注解不是编译器将接口识别为功能接口的必要条件,而仅是帮助捕获设计意图并获得编译器帮助识别意外违反设计意图的帮助。
1.Funtion源码阅读
/**
* Represents a function that accepts one argument and produces a result.
* 表示接受一个参数并生成结果的函数。
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
*
* @param <T> the type of the input to the function 函数输入的类型
* @param <R> the type of the result of the function 函数结果的类型
*
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
* 将此函数应用于给定参数
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
* 返回一个组合函数,该函数首先应用{@code before}
* 函数,然后将此函数应用于结果。
* 如果对任一函数的求值引发异常,则会将其转发到
* 组合函数的调用者。
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* 返回首先将此函数应用于的组合函数
* 然后将{@code after}函数应用于结果。
* 如果对任一函数的求值引发异常,则会将其转发到
* 组合函数的调用者。
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
* 返回始终返回其输入参数的函数。
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
2.Funtion函数汇总
接口方法 | 方法描述 |
R apply(T t) | 将此函数应用于给定参数 |
Function<T, R> andThen(Function<? super R,? extends V> after) | 返回一个组合函数,该函数结果应用到after函数中 |
Function<T, R> compose(Function<? super V,? extends T> before) | 返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中 |
3.Funtion函数举例
public class FunctionTest {
public static void main(String[] args) {
//1.将此函数应用于给定参数
apply();
//2.返回一个组合函数,该函数结果应用到after函数中
andThen();
//3.返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中
compose();
}
//将此函数应用于给定参数
public static void apply() {
Function<String, String> function = a -> a + " Girl!";
System.out.println(function.apply("Hello")); // Hello Girl!
}
//返回一个组合函数,该函数结果应用到after函数中
public static void andThen() {
Function<String, String> function = a -> a + " Girl!";
Function<String, String> function1 = a -> a + " Boy!";
String greet = function.andThen(function1).apply("Hello");
System.out.println(greet); // Hello Girl! Boy!
}
//返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中
public static void compose() {
Function<String, String> function = a -> a + " Girl!";
Function<String, String> function1 = a -> a + " Boy!";
String greet = function.compose(function1).apply("Hello");
System.out.println(greet); // Hello Boy! Girl!
}
}
以上是关于JDK中Funtion接口源码解析和使用详解的主要内容,如果未能解决你的问题,请参考以下文章