Zip Collections

161 views
Skip to first unread message

dtiw...@gmail.com

unread,
Apr 3, 2017, 10:27:47 AM4/3/17
to guava-discuss
Is there any utility which could allow us to zip two collections in Guava?

Olivier Grégoire

unread,
Apr 3, 2017, 10:34:40 AM4/3/17
to dtiw...@gmail.com, guava-discuss
It's often asked and there's an issue opened for it: https://github.com/google/guava/issues/677

Regards,
Olivier

Le lun. 3 avr. 2017 à 16:27, <dtiw...@gmail.com> a écrit :
Is there any utility which could allow us to zip two collections in Guava?

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/a2f05a7a-24e6-421e-9376-5eabcbb772da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pete Gillin

unread,
Apr 4, 2017, 2:00:23 PM4/4/17
to Olivier Grégoire, dtiw...@gmail.com, guava-discuss
Guava 21 introduced Streams.zip, which takes a Stream<A> and a Stream<B> and a BiFunction<? super A,? super B,R> which gives you a Stream<R>. If you have Collections, you could stream both of them, and if you need a Collection back rather than a Stream you could collect it, say using toImmutableList. (The problem with doing this directly in Collections is that there's no good return type since Guava has no Tuple types. It's better for your code, via the BiFunction, to state how the elements ought to be combined.)

Pete.

Tim Peierls

unread,
Apr 4, 2017, 2:43:54 PM4/4/17
to Pete Gillin, Olivier Grégoire, dtiw...@gmail.com, guava-discuss
StreamEx has good support for this sort of thing:


and other zip methods.

StreamEx and Guava play pretty well together.

--tim

On Tue, Apr 4, 2017 at 1:58 PM, 'Pete Gillin' via guava-discuss <guava-...@googlegroups.com> wrote:
Guava 21 introduced Streams.zip, which takes a Stream<A> and a Stream<B> and a BiFunction<? super A,? super B,R> which gives you a Stream<R>. If you have Collections, you could stream both of them, and if you need a Collection back rather than a Stream you could collect it, say using toImmutableList. (The problem with doing this directly in Collections is that there's no good return type since Guava has no Tuple types. It's better for your code, via the BiFunction, to state how the elements ought to be combined.)

Pete.
On Mon, 3 Apr 2017 at 15:34 Olivier Grégoire <ogre...@gmail.com> wrote:
It's often asked and there's an issue opened for it: https://github.com/google/guava/issues/677

Regards,
Olivier

Le lun. 3 avr. 2017 à 16:27, <dtiw...@gmail.com> a écrit :
Is there any utility which could allow us to zip two collections in Guava?

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discuss+unsubscribe@googlegroups.com.

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discuss+unsubscribe@googlegroups.com.

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/CAOSPF%2Be7txTVBN8m-7w50LyE-KFgxMWyBPdu%3DBh%3DdDoYHk2mFA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages