1. Overview

In this tutorial, we’ll convert a Map to a Spring MultiValueMap and understand it with a clear example.

In the Spring Framework, a MultiValueMap is a specialized map that holds multiple values against a single key. It’s beneficial for handling HTTP request parameters, headers, and in other scenarios where one key might correspond to multiple values. Converting a standard Map to a MultiValueMap can be a common requirement in Spring applications.

2. What Is a MultiValueMap?

A MultiValueMap is an interface Spring provides that extends the Map<K, List<V>> interface. It allows us to store multiple values against a single key.

The most commonly used implementation is LinkedMultiValueMap.

3. Ways to Convert a Map to a Spring MultiValueMap

Let’s say we have a Map<String, List<String>> and we want to convert it to a MultiMap<String, String>.

Here’s how we can do that.

We should ensure that our project includes the Spring Framework. If we’re using Maven, we add the following dependency to our pom.xml:

<dependency>
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId>
    <version>6.1.7</version>
</dependency>

As, an initial step, we’ll create a sample Map<String, List<String>> with some data:

Map<String, List<String>> map = new HashMap<>();
map.put("rollNo", Arrays.asList("4", "2", "7", "3"));
map.put("name", Arrays.asList("John", "Alex", "Maria", "Jack"));
map.put("hobbies", Arrays.asList("Badminton", "Reading novels", "Painting", "Cycling"));

3.1. Using Manual Iteration

We can convert our Map to MultiValueMap using normal iteration:

MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    multiValueMap.put(entry.getKey(), entry.getValue());
}

The code above converts a Map<String, List<String>> into a MultiValueMap<String, String>. We initialize a new LinkedMultiValueMap and iterate over each entry in the original map, adding each key and its associated list of values to the MultiValueMap.

This structure is particularly useful in Spring MVC for handling form data or query parameters with multiple values associated with a single key.

3.2. Using CollectionUtils.toMultiValueMap()

The Spring Framework provides a utility method in CollectionUtils that can directly convert a Map<K, List<V>> to a MultiValueMap<K,V>:

MultiValueMap<String, String> multiValueMap = CollectionUtils.toMultiValueMap(map);

3.3. Using Java Streams

Java Streams API can also be utilized to perform the conversion in a more functional programming style:

MultiValueMap<String, String> multiValueMap = map.entrySet()
  .stream()
  .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedMultiValueMap::new));

This code converts a Map<String, List<String>> to a MultiValueMap<String, String> using Java Streams. It collects the map entries into a new LinkedMultiValueMap, preserving the original map’s key-value structure.

4. Conclusion

In this article, we saw how to convert a Map to a MultiValueMap. By following the steps outlined above, we can easily convert and utilize MultiValueMap in our Spring applications, making our code more versatile and easier to manage when dealing with complex data structures.

We saw multiple ways, and each way has its advantages. It’s important to choose the method that best fits our coding style and application requirements. Each of these approaches will help us efficiently handle scenarios where a key maps to multiple values in Spring applications.

The source code of all these examples is available over on GitHub.

Course – RWSB – NPI (cat=REST/Spring/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until the 22nd of June, after which it will permanently increase by 50$

>> GET ACCESS NOW

Course – LS (cat=Spring)
announcement - icon

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

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments