I have a List
and want a Guava MultimapFoo
s by each tag of their Collection
You can use Stream.flatMap to create a stream on each tag/foo pair, and then collect it into a multimap. Newer versions of Guava provide collectors such as ImmutableSetMultimap.toImmutableSetMultimap and ImmutableListMultimap.toImmutableListMultimap for this purpose.
ImmutableSetMultimap result = list.stream()
.flatMap(foo -> foo.getTags().stream().map(tag -> Map.entry(tag, foo)))
.collect(ImmutableSetMultimap.toImmutableSetMultimap(Map.Entry::getKey, Map.Entry::getValue));