Difference Between Map and MultivaluedMap in Java – Java中Map和MultivaluedMap的区别

最后修改: 2022年 11月 11日

1. Overview


In this tutorial, we’ll learn the difference between Map and MultivaluedMap in Java. But before that, let’s take a look at some examples.


2. Example for Map


HashMap implements the Map interface, and it also permits null values and null keys:


public void givenHashMap_whenEquals_thenTrue() {
    Map<String, Integer> map = new HashMap<>();

    // Putting key-value pairs into our map.
    map.put("first", 1);
    map.put(null, 2);
    map.put("third", null);

    // The assert statements. The last arguments is what's printed if the assertion fails.
    assertNotNull(map, "The HashMap is null!");
    assertEquals(1, map.get("first"), "The key isn't mapped to the right value!");
    assertEquals(2, map.get(null), "HashMap didn't accept null as key!");
    assertEquals(null, map.get("third"), "HashMap didn't accept null value!");

The above unit test passes successfully. As we can see, each key is mapped to exactly one value.


3. How to Add MultivaluedMap to Our Project?


Before using the MultivaluedMap interface and its implementing classes, we need to add its library, Jakarta RESTful WS API, to our Maven project. To do that, we need to declare a dependency in the project’s pom.xml file:

在使用MultivaluedMap接口及其实现类之前,我们需要将其库Jakarta RESTful WS API加入我们的Maven项目。为此,我们需要在项目的pom.xml文件中声明一个依赖项。


We’ve successfully added the library to our project.


4. Example for MultivaluedMap


MultivaluedHashMap implements the MultivaluedMap interface, and it permits null values and null keys:


public void givenMultivaluedHashMap_whenEquals_thenTrue() {
    MultivaluedMap<String, Integer> mulmap = new MultivaluedHashMap<>();

    // Mapping keys to values.
    mulmap.addAll("first", 1, 2, 3);
    mulmap.add(null, null);

    // The assert statements. The last argument is what's printed if the assertion fails.
    assertNotNull(mulmap, "The MultivaluedHashMap is null!");
    assertEquals(1, mulmap.getFirst("first"), "The key isn't mapped to the right values!");
    assertEquals(null, mulmap.getFirst(null), "MultivaluedHashMap didn't accept null!");

The above unit test passes successfully. Here, each key is mapped to a list of values that can contain zero or more elements.


5. What are the Differences?


In the Java ecosystem, Map and MultivaluedMap are both interfaces. The difference is that, in a Map, every key is mapped to exactly one object. However, in a MultivaluedMap, we can have zero or more objects associated with the same key.


Further, MultivaluedMap is a subinterface of Map, so it has all of its methods and a few more of its own. Some of the classes that implement Map are HashMap, LinkedHashMap, ConcurrentHashMap, WeakHashMap, EnumMap, and TreeMap. Moreover, MultivaluedHashMap is a class that implements both Map and MultivaluedMap.

此外,MultivaluedMapMap的一个子接口,所以它拥有它的所有方法和一些自己的方法。实现Map的一些类是HashMap, LinkedHashMap, ConcurrentHashMap, WeakHashMap, EnumMap, 和TreeMap。此外,MultivaluedHashMap是一个同时实现了MapMultivaluedMap的类。

For instance, the addFirst(K key, V value) is one of MultivaluedMap‘s methods that adds a value to the first position in the current list of values for the supplied key:

例如,addFirst(K key, V value)MultivaluedMap的方法之一,它为所提供的键的当前值列表中的第一个位置添加一个值。

MultivaluedMap<String, String> mulmap = new MultivaluedHashMap<>();
mulmap.addFirst("firstKey", "firstValue");

On the other hand, getFirst(K key) gets the first value of the supplied key:

另一方面,getFirst(K key)获得所提供的键的第一个值。

String value = mulmap.getFirst("firstKey");

And finally, addAll(K key, V… newValues) adds multiple values to the current list of values for the supplied key:

最后,addAll(K key, V… newValues)将多个值添加到提供的键的当前值列表中。

mulmap.addAll("firstKey", "secondValue", "thirdValue");

6. Summary


In this article, we saw the differences between Map and MultivaluedMap.


As always, the complete source code is available over on GitHub.