Open In App

MultiMap in Java Guava

Introduction : A Multimap is a general way to associate keys with arbitrarily many values. Guava’s Multimap framework makes it easy to handle a mapping from keys to multiple values. There are two ways to think of a Multimap conceptually :
1) As a collection of mappings from single keys to single values.

a -> 1
a -> 2
a -> 4
b -> 3
c -> 5

2) As a mapping from unique keys to collections of values.



a -> [1, 2, 4]
b -> [3]
c -> [5] 

Declaration : The declaration of com.google.common.collect.Multimap<K, V> interface is as :

@GwtCompatible
public interface Multimap

Below given is the list of some methods provided by Guava’s Multimap Interface :


Views : Multimap also supports a number of powerful views. Much of the power of the multimap API comes from the view collections it provides. These always reflect the latest state of the multimap itself.



Some other methods provided by Guava’s Multimap Interface are :

Multimap V/s Map : A Multimap<K, V> is not a Map<K, Collection<V>>, though such a map might be used in a Multimap implementation. Below given are the differences :

Implementations : Multimap provides a wide variety of implementations. You can use it in most places you would have used a Map<K, Collection<V>>.

Advantages : Multimaps are commonly used in places where a Map<K, Collection<V>> would otherwise have appeared.

References :
Google Guava

Article Tags :