javase-java8-function Composite Usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.