At one place i have to use a map with many values mapped to a single key, so i was wondering whether there is any significant performance distinction between using HashMap of key, list and MultiMap of key , values in java.
A MultiValueMap decorates another map, allowing it to have more than one value for a key. A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.
A map cannot contain duplicate keys; each key can map to at most one value. So in a MultivaluedMap you can insert 0, 1, 2, 3 or more objects related to the same key. In a Map you can insert exactly 1 object related to a key.
A Multimap is a new collection type that is found in Google's Guava library for Java. A Multimap can store more than one value against a key. Both the keys and the values are stored in a collection, and considered to be alternates for Map<K, List<V>> or Map<K, Set<V>> (standard JDK Collections Framework).
Method SummaryAdd the given single value to the current list of values for the given key. Add all the values of the given list to the current list of values for the given key. Add all the values of the given MultiValueMap to the current values. Add the given value, only when the map does not contain the given key.
You can try it but I doubt there is much difference as it does much the same thing.
IMHO The advantage is simpler/clearer code which is usually more important than performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With