Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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:

@Test
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:

<dependency>
    <groupId>jakarta.ws.rs</groupId>
    <artifactId>jakarta.ws.rs-api</artifactId>
    <version>3.1.0</version>
</dependency>

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:

@Test
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.

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:

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:

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:

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.