What use is Java 6 interface MultivaluedMap?

Malatesh picture Malatesh · Mar 11, 2014 · Viewed 24.6k times · Source

What use is Java 6 interface MultivaluedMap?

Answer

Rafael Winterhalter picture Rafael Winterhalter · Mar 11, 2014

The interface does not belong to "Java", meaning that the interface is not a part of the core libraries. It is a part of the javax.ws.rs hierarchy which is part of the JAX-RS specification. It is used by frameworks implementing the specification such as Jersey. It is used whenever maps should refer to not only a single value but to any number of values. An example for the use would be for example the storage of a request header where you one might want to add several values per key. Or even no keys in some cases where it is easier to handle an empty list compared to a null value.

Take this HTTP-header for example:

Accept-Encoding: compress;q=0.5, gzip;q=1.0

You would model this by

MultivaluedMap<String, String> map = ...
map.add("Accept-Encoding", "compress;q=0.5");
map.add("Accept-Encoding", "gzip;q=1.0");

internally in Jersey. This type of multiple value storage is a common problem in Java that is addressed by other implementors of maps such as Guava.

This is basically what the javadoc says:

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