Search code examples
javacollectionsguava

What is difference between HashMap and HashMultimap


I see many examples about multimap but did not understand why Google Gauva is different?

Multimap<Integer, Set<String>> option4 = HashMultimap.create(); // Gauva

Map<Integer, Set<String>> opt = new HashMap<Integer, Set<String>>(); //Core Java

Is both above are behave same for holding data or different?


Solution

  • A MultiMap<A, B> associates a key of type A with a value of type Collection<B> (hence the name MultiMap)

    A Map<A, B> associates a key of type A with a value of type B.

    So, a MultiMap<Integer, Set<String>> can be viewed as a Map<Integer, Collection<Set<String>>. This should be obvious by reading the api documentation.