A collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys tononempty collections of values:

  • a → 1, 2
  • b → 3

… or as a single “flattened” collection of key-value pairs:

  • a → 1
  • a → 2
  • b → 3

Important: although the first interpretation resembles how most multimaps are implemented, the design of the Multimap API is based on the second form. So, using the multimap shown above as an example, the size() is 3, not 2, and the values() collection is [1, 2, 3], not [[1, 2], [3]]. For those times when the first style is more useful, use the multimap’s asMap()view (or create a Map<K, Collection<V>> in the first place).

Using the standard Java Collections that’s usually accomplished by using another collection as the value type. This unfortunately ends up involving a lot of ceremony that needs to be repeated in terms of initializing the collection. Multimaps clear this up quite a bit

Result:

Visit My Repository for full code
showimage

Reference

  1. MultiMap Google Guava
  2. Five Reasons to use Guava

 

Tagged with: