Combine two Futures into one, without changing value type?

2,590 views
Skip to first unread message

googl...@schildbach.de

unread,
May 8, 2016, 6:24:25 AM5/8/16
to guava-discuss
 I have two futures with the same value T which I want to combine into one. Futures.allAsList() seems to do what I want but it changes the value of the combined future from T to List<T>. Is there something similar that doesn't change the value type?

See https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/Peer.java#L163

amertum

unread,
May 9, 2016, 4:15:32 AM5/9/16
to guava-discuss, googl...@schildbach.de

Chris Povirk

unread,
May 9, 2016, 5:40:52 AM5/9/16
to googl...@schildbach.de, guava-discuss
For now, you can call transform() on the result of allAsList().

Once Guava 20 is out (in the next few months), you can do something like:
Futures.whenAllSucceed(a, b).call(-> doSomething(a, b));

googl...@schildbach.de

unread,
May 9, 2016, 7:04:23 AM5/9/16
to guava-discuss, googl...@schildbach.de
Thanks for the hint. What type is transform() a method of? I've been looking in ListenableFuture and all subtypes but could not find anything.

Chris Povirk

unread,
May 9, 2016, 9:09:08 AM5/9/16
to googl...@schildbach.de, guava-discuss
transform(), along with other ListenableFuture utilities, are available in Futures.java.
Reply all
Reply to author
Forward
0 new messages