The difference between thenapply () and Thencompose ()

Source: Internet
Author: User

The difference between thenapply () and Thencompose ():

Thenapply () is a non-completablefuture type returned:
its function is equivalent to converting completablefuture<t> into completablefuture<u>.

Thencompose () is used to connect two completablefuture, and the return value is the new completablefuture:

Summary: thenapply () conversion is the type of the generic, is the same completablefuture;
Thencompose () is used to connect two completablefuture and is to generate a new completablefuture.


Example:
1.thenApply ():

completablefuture<integer> future = Completablefuture.supplyasync ((), {

return 100;

});

completablefuture<string> f = future.thenapplyasync (i-i). thenapply (i-i.tostring ());

System.out.println (F.get ()); "1000"

Thenapply () Source code:

Public <U> completablefuture<u> Thenapply (
function<? Super T,? Extends u> fn) {
return uniapplystage (NULL, FN);
}

Interpretation:
Parameters: function<? Super T,? Extends u> fn)
Enter the value of T in the preceding completablefuture<t>, return the value of the type that inherits U, return this value

Order: First determine the return value U or its subclass, and then determine the <U> type in the method in public <U> completablefuture<u> thenapply ()

2.thenCompose ():

completablefuture<integer> future = Completablefuture.supplyasync ((), {

return 100;

});

completablefuture<string> f = future.thencompose (I, {

Return Completablefuture.supplyasync ((), {

Return (I * 10) + "";

});

});

System.out.println (F.get ()); 1000

The difference between thenapply () and Thencompose ()

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.