Several important function interfaces in Java 8
1. Function < T, R > function interface
/**
- @Author: cxh
- @CreateTime: 17/12/23 21:09
- @ProjectName: JavaBaseTest
-
<>
*/
import java.util.Objects;
/**
-
Represents a function that accepts one argument and produces a result.
-
接受一个参数,并返回一个结果值.
-
This is a
-
whose functional method is {@link #apply(Object)}.
-
这是一个函数接口,它的函数方法为:apply
-
@param
the type of the input to the function -
@param
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.
- 此方法返回一个组合函数.
- 这一函数的执行过程:先对参数执行before函数,然后对结果执行apply函数.
- before函数和apply函数,任一函数执行出现异常,则异常会被转到组合函数compose那里.
- ----本函数:体现了前套关系
- @param
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)
*/
defaultFunction<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.
- andThen方法,返回一个组合函数.
- andThen方法的执行过程:先对输入参数执行apply函数,然后对结果执行after函数.
- apply函数和apply函数,则异常会被转到这一组合函数的调用者那里.
- ----本函数:转换了嵌套的顺序
- @param
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)
*/
defaultFunction<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
the type of the input and output objects to the function - @return a function that always returns its input argument
*/
staticFunction<T,T> identity() {
return t -> t;
}
}
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
//Function
Function<String,Integer> function1= s -> s.length();
//1.功能方法:apply
System.out.println(function1.apply("LiJin"));//2.默认方法:compose Function<Integer,String> function2=integer->String.valueOf(integer); System.out.println(function2.compose(Integer::lowestOneBit).apply(100)); //3.默认方法:andThen Function<Integer,String> function3=i-> { if(i==5) return "LiJin"; else return "LiLy"; }; System.out.println(function3.andThen(String::new).apply(11)); System.out.println(function3.andThen(String::new).apply(5)); //4.静态方法:identity System.out.println(Function.identity().apply(111)); }
}
//输出
5
4
LiLy
LiJin
111
Process finished with exit code 0
/**
- @Author: cxh
- @CreateTime: 17/12/23 21:32
- @ProjectName: JavaBaseTest
*/import java.util.Objects;
/**
-
Represents a function that accepts two arguments and produces a result.
-
This is the two-arity specialization of {@link java.util.function.Function}.
-
这是一个二元函数,二元函数没有compose能力.
-
函数功能:输入两个参数,返回一个结果.
-
This is a
-
whose functional method is {@link #apply(Object,Object)}.
-
@param
the type of the first argument to the function -
@param the type of the second argument to the function
-
@param
the type of the result of the function -
@see java.util.function.Function
-
@since 1.8
*/
@FunctionalInterface
public interface BiFunction<T,R> {/**
- Applies this function to the given arguments.
- 处理2个输入参数的方法:apply
- @param t the first function argument
- @param u the second function argument
- @return the function result
*/
R apply(T t,U u);
/**
- Returns a composed function that first applies this function to
- its input,it is relayed to
- the caller of the composed function.
- addThen方法:返回一个组合函数.
- 函数执行过程:先对输入参数执行apply函数,再对apply的结果执行after函数.
- 如果执行过程中,任一函数发生异常,异常会被返回到组合函数的调用者这里.
- @param
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
*/
defaultBiFunction<T,V> andThen(java.util.function.Function<? super R,? extends V> after) {
Objects.requireNonNull(after);
return (T t,U u) -> after.apply(apply(t,u));
}
}