Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between HashMap and HashMultimap

Tags:

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?

like image 956
Faisal Basra Avatar asked Oct 07 '13 10:10

Faisal Basra


People also ask

What is difference between HashMap and Map?

HashMap is a non-synchronized class of the Java Collection Framework that contains null values and keys, whereas Map is a Java interface, which is used to map key-pair values.

What is difference between HashMap and HashTable with example?

HashMap is non-syncronized and is not thread safe while HashTable is thread safe and is synchronized. HashMap allows one null key and values can be null whereas HashTable doesn't allow null key or value. HashMap is faster than HashTable. HashMap iterator is fail-safe where HashTable iterator is not fail-safe.

What is difference between HashMap and HashSet?

HashMap Stores elements in form of key-value pair i.e each element has its corresponding key which is required for its retrieval during iteration. HashSet stores only objects no such key value pairs maintained. Put method of hash map is used to add element in hashmap.

What can be used instead of HashMap?

ArrayList stores the elements only as values and maintains internally the indexing for every element. While HashMap stores elements with key and value pairs that means two objects. So HashMap takes more memory comparatively.


1 Answers

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.

like image 194
JB Nizet Avatar answered Sep 29 '22 22:09

JB Nizet