Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap vs MultivaluedMap

Tags:

java

rest

jersey

I'm recently into Rest API in java and came across MultivaluedMap. I don't find the advantage MultivaluedMap over HashMap or other classes like TreeMap or LinkedHashMap.Please explain the scenario when MultivaluedMap is necessary.

like image 772
Soa Fusion Avatar asked Feb 06 '19 11:02

Soa Fusion


People also ask

What is difference between Map and MultiValueMap?

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.

What is MultiValuedMap?

A MultiValuedMap 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.

What is the best alternative for HashMap in Java?

Whereas, ConcurrentHashMap is introduced as an alternative to the HashMap. The ConcurrentHashMap is a synchronized collection class. The HashMap is non-thread-safe and can not be used in a Concurrent multi-threaded environment.

Is HashMap better than Map?

HashMap is faster than Map since it does not keep track of the order in which its components are inserted. HashMap, unlike Map, can hold duplicate values. It's feasible to use the Map interface implementing classes to implement it. HashMap, on the other hand, is all about implementing the Map interface.


1 Answers

A MultivaluedMap is:

A map of key-values pairs. Each key can have zero or more values.

A Map is:

An object that maps keys to values. 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.

This is the difference, it can be useful if you need to store many values related to a single key, if you have only one value they are similar.


A possibile scenario is a Dictionary where you save all the words related starting with a letter. The keys are A B C... Z and the values are the words.

A -> Acid
     Apple 
     August

B -> Banana
     Boat

C -> Car
     Chain

...

Z -> Zebra

That can be coded with:

MultivaluedMap<String, String> multiMap = new MultivaluedHashMap<>();
multiMap.add("A", "Acid");
multiMap.add("A", "Apple");
multiMap.add("A", "August");
multiMap.add("B", "Banana");
multiMap.add("B", "Boat");
multiMap.add("C", "Car");
multiMap.add("C", "Chain");
multiMap.add("Z", "Zebra");

Another scenario is a Map holding all products buyed by a user. The keys are the user id and values are the products.

USER1 -> Pizza Margherita
         Pizza Pepperoni

USER2 -> Pizza Margherita

USER3 -> Pizza Margherita
         Pizza Pepperoni
         Pizza Four Cheese
like image 140
Davide Lorenzo MARINO Avatar answered Sep 28 '22 05:09

Davide Lorenzo MARINO