The functional interface function of the JDK8 has two default methods, Andthen and compose, which return an instance of the function, which can be used to compound the lambda expression represented by the function interface.
Let's look at a simple example:
Function<integer, integer> f = x, X + 1; Function<integer, integer> g = x, X * 2;//andthe usageFunction<integer, integer> h =F.andthen (g);//(2+1) * = 6System.out.println (H.apply (2));//(5+1) *2=12System.out.println (H.apply (5));//Compose UsageFunction<integer, integer> p =F.compose (g);//(3*2) +1=7System.out.println (P.apply (3));//(9*2) +1=19System.out.println (p.apply (9));
Easy to use
Packageorg.burning.sport.javase.lambda.chapter3.function; Public classLetter { Public Staticstring AddHeader (string text) {return"From Zhejianghz" +text; } Public Staticstring Addfooter (string text) {return"Sign Yingjieli" +text; } Public Staticstring Checkspell (string text) {returnText.replace ("Li", "Zhang"); }}
/***/Function<string, string> header = letter::addheader; Function<string, string> pipeLine = header.andthen (letter::addfooter). Andthen (Letter::checkspell); System.out.println (pipeline.apply ("wangying")); Function<string, string> pipeLine2 = header.compose (letter::addfooter); System.out.println (pipeline2.apply ("Jiahao"));
Https://gitee.com/play-happy/base-project Reference:
"1" "Java8 Combat"
javase-java8-function Composite Usage