javamergejava-streamguavamultimap

Merge a list of Guava Multimaps


Is there a way to elegantly merge a list of Guava Multimaps with the same key/value pairs in Java 8?

I have tried using .collect(Multimaps.toMultimap()) with no luck.


Solution

  • There are a few ways; this is the cleanest one I could find:

    list.stream().collect(ArrayListMultimap::create, Multimap::putAll, Multimap::putAll)
    

    Feel free to replace ArrayListMultimap with some other implementation.