Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, 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.

Generic bottom

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

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments