Convert a Map<K, Collection<V>> to Guava’s Multimap in Java
Guava provides a Multimap interface that allows mapping of a single key to multiple values in Java. This post will discuss how to convert a Map<K, Collection<V>> to Guava’s Multimap in Java.
We can create our own custom implementation of Multimap in Java using a Map and a Collection by implementing the map as Map<K, Collection<V>>. We might often want to use a third-party library for Multimap in Java, such as Guava. There are several ways to convert a Map<K, Collection<V>> to Guava’s Multimap:
1. Using Map.entrySet() with ListMultimap.put() method
A naive solution is to iterate over the mappings contained in the map using Map.entrySet() and for each value in a mapping, use the ListMultimap.put() method to insert that key-value pair into a multimap.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Convert `Map<K, Collection<V>>` to Guava's `Multimap` in Java */ public static <K, V> ListMultimap<K, V> createMultiMap(Map<K, Collection<V>> map) { ListMultimap<K, V> multimap = ArrayListMultimap.create(); for (Map.Entry<K, Collection<V>> entry: map.entrySet()) { Collection<V> values = entry.getValue(); for (V value: values) { multimap.put(entry.getKey(), value); } } return multimap; } |
2. Using Map.entrySet() with ListMultimap.putAll() method
Instead of using the ListMultimap.put() method to insert key-value pairs into a multimap one by one, we can use the ListMultimap.putAll() method to insert all values mapped to a particular key all at once.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Convert `Map<K, Collection<V>>` to Guava's `Multimap` in Java */ public static <K, V> ListMultimap<K, V> createMultiMap(Map<K, Collection<V>> map) { ListMultimap<K, V> multimap = ArrayListMultimap.create(); for (Map.Entry<K, Collection<V>> entry: map.entrySet()) { multimap.putAll(entry.getKey(), entry.getValue()); } return multimap; } |
3. Using Map.keySet() with ListMultimap.putAll() method
Another alternative is to use the Map.keySet() to iterate over the keys contained in the map, and for each key, use the ListMultimap.putAll() method to insert a key-value pair into a multimap.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Convert `Map<K, Collection<V>>` to Guava's `Multimap` in Java */ public static <K, V> ListMultimap<K, V> createMultiMap(Map<K, Collection<V>> map) { ListMultimap<K, V> multimap = ArrayListMultimap.create(); for (K key: map.keySet()) { multimap.putAll(key, map.get(key)); } return multimap; } |
4. Using Map.forEach() with ListMultimap.putAll() method
In Java 8, we can use Map.forEach() to iterate over each mapping in the map and pass method reference to ListMultimap.putAll() as argument to forEach().
|
1 2 3 4 5 6 7 8 9 |
/** * Convert `Map<K, Collection<V>>` to Guava's `Multimap` in Java */ public static <K, V> ListMultimap<K, V> createMultiMap(Map<K, Collection<V>> map) { ListMultimap<K, V> multimap = ArrayListMultimap.create(); map.forEach(multimap::putAll); // Java 8 and above return multimap; } |
5. Creating an Immutable Multimap
Guava also provides Immutable Multimap whose contents can never change. We can use similar logic, as discussed above, to construct an immutable multimap instance with the help of a builder.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * Creates an Immutable Guava `Multimap` from `Map<K, Collection<V>>` */ public static <K, V> ImmutableMultimap<K, V> createMultiMap(Map<K, Collection<V>> map) { ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder(); for (Map.Entry<K, Collection<V>> entry: map.entrySet()) { builder.putAll(entry.getKey(), entry.getValue()); } return builder.build(); } |
That’s all about converting a Map<K, Collection<V>> to Guava’s Multimap in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)