Java 8 Collector for Gauva’s LinkedHashMultimap

10 months ago or so I wrote a blog post entitled Immutable List Collector in Java 8 (that probably should have been named “Unmodifiable List Collector in Java 8” to be more precise) that presented a way to turn a Stream into an unmodifiable list. I was recently asked if I could present a Collector for the LinkedHashMultimap in Guava which is what I will do here.

The LinkedHashMultimapCollector

In it’s simplest form it can look like this:

The implementation is quite similar to what I did in the previous blog so read up on the details there. This allows us to turn a Streams of entries (key, value) into a LinkedHashMultimap. For example:

Making it more generic

While this works for LinkedHashMultimap it would be nice to make the collector work for other instances of Multimap. So let’s modify the collector to do so:

Now the toMultimap method accepts a Supplier that creates a user defined implementation of a Multimap. As an example consider if we want to return a ArrayListMultimap instead of a LinkedHashMultimap in the test case from the previous example. With the more generic collector in place we could now do like this:

To use the toMultimap method to create a LinkedHashMultimap we simply create a supplier for this type instead:

Of course we could easily extend the MultimapCollector class with a new method that explicitly return us a LinkedHashMultimap:

Conclusion

Once you see a Collector implementation like the ones in this blog they don’t look all that complex in the end. But in my experience it can be hard to get the types and generics right and even a seemingly quite straight-forward collector can take a while to get right.

Leave a Reply

Close Menu