Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm reading the document on CompletableFuture and The description for thenAccept() is

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action.

and for thenApply() is

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function.```

Can anyone explain the difference between the two with some simple examples?

question from:https://stackoverflow.com/questions/45174233/difference-between-thenaccept-and-thenapply

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
255 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to look at the full method signatures:

CompletableFuture<Void>     thenAccept(Consumer<? super T> action)
<U> CompletableFuture<U>    thenApply(Function<? super T,? extends U> fn)

thenAccept takes a Consumer and returns a T=Void CF, i.e. one that does not carry a value, only the completion state.

thenApply on the other hand takes a Function and returns a CF carrying the return value of the function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...