Let's get started with a Microservice Architecture with Spring Cloud:
Add Values to ArrayList Used as Value in HashMap in Java
Last updated: March 7, 2026
1. Overview
In this tutorial, we’ll explore using a Map in Java to store several values for a single key. One way to do this is to use a List as the Map value type and add the elements there, with a data structure of the form Map<K, List<V>>.
We’ll demonstrate using a HashMap and ArrayList, though you can use any other Map and List implementations instead. Alternatively, we can use external libraries that support data structures with multiple values. We’ll cover three ways to implement this approach, including how to safely add multiple values to the same key.
2. Manual Handling of Keys, Values, and List Elements
With this first approach, we’ll handle everything ourselves and add the keys manually:
public static Map<String, List> addKeyManually(Map<String, List> map, String key, String value) {
if (!map.containsKey(key)) {
map.put(key, new ArrayList());
}
map.get(key).add(value);
return map;
}
We’re manually checking if the HashMap contains the key that we’re trying to add to ensure there’s a valid ArrayList to store our values. If the key doesn’t contain a valid List, we will assign one to the dictionary with the put method, having map.put(key, new ArrayList<>()).
Once we know there is a valid List in our map for the given key, we can add values manually to the List. We can retrieve this List with the get method as map.get(key). Then, with the List we can use the add method to add the value, ending up with map.get(key).add(value).
We can directly use this method on an empty HashMap:
Map<String, List<String>> map = new HashMap<>();
ArrayListInHashMap.addKeyManually(map, K1, K1_V1);
ArrayListInHashMap.addKeyManually(map, K1, K1_V2);
ArrayListInHashMap.addKeyManually(map, K2, K2_V1);
This will construct a map that looks like {key1=[key1_value1, key1_value2], key2=[key2_value1]}. We can see that not every List needs to contain the same number of values.
This solution is a bit verbose, but it’s very generic because we can use it in any Java version (even Java 5), and it helps understand the basic logic of this implementation.
However, this version is less computationally optimal than the following solutions. Here we’re performing two or three lookups. Two lookups are done with the containsKeyand with the get method. We can run into a third lookup if we perform the put operation. We can optimize the code with a null check to avoid the calls to both containsKey and get.
3. Leveraging the computeIfAbsent Method
Since Java 8, we can use the method computeIfAbsent of the Map interface. We can use this computeIfAbsent method to retrieve the value for a given key in a map by passing the key and, if this key is absent, a mapping to obtain a new value and insert it into the map:
public static Map<String, List> addKeyWithComputeIfAbsent(
Map<String, List> map, String key, String value) {
map.computeIfAbsent(key, k -> new ArrayList()).add(value);
return map;
}
By using computeIfAbsent we don’t need to manually perform the containsKey checks, since this is automatically performed with the initialization.
Again, we can construct the same map as we constructed before:
Map<String, List<String>> map = new HashMap<>;
ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K1, K1_V1);
ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K1, K1_V2);
ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K2, K2_V1);
The main advantage of this code is that we’re automating and optimizing operations with computeIfAbsent, which we were doing manually before. computeIfAbsent does a single lookup versus the two or three lookups from the previous version, as it searches for the key once and decides what to do while it’s already at that memory location. However, we do need a version of Java newer than Java 8 for computeIfAbsent to be available.
4. With External Libraries
Of course, storing several values for a single key in a HashMap is a problem that we often encounter. Thus, we can rely on data structures that external libraries provide to solve this problem. We’ll cover the three most popular ones so that we can use the one we’re already using in our codebase.
4.1. MultiValuedMap From Apache Commons Collections
The Apache Commons Collections library provides the MultiValuedMap interface that allows us to store several values for a single key:
public static MultiValuedMap<String, String> addKeyToApacheMultiValuedMap(
MultiValuedMap<String, String> map, String key, String value) {
map.put(key, value);
return map;
}
This looks like previous approaches, but we don’t need to manage the ArrayList part manually, and we just call the put method that handles everything for us. There are many other helper methods for us to interact with the content of our map easily.
4.2. LinkedMultiValueMap From Spring
Another popular library with support for maps with several values for a single key is Spring. It provides the interface LinkedMultiValueMap to achieve our objective:
public static MultiValueMap<String, String> addKeyToSpringLinkedMultiValueMap(
MultiValueMap<String, String> map, String key, String value) {
map.add(key, value);
return map;
}
We can see that the map type is declared using two strings, MultiValueMap<String, String>, since it’s a LinkedMultiValueMap that takes care of handling values with the same key. Before, we were using a HashMap that had a String for each key and a corresponding List<String> as the value.
4.3. Multimap From Google Guava
Finally, another popular library is Google’s Guava, which provides the Multimap interface to have a map with keys storing several values:
public static Multimap<String, String> addKeyToGuavaMultimap(
Multimap<String, String> map, String key, String value) {
map.put(key, value);
return map;
}
We see that the syntax is very similar for all external libraries. The Guava library is very efficient, and it’s widely used in many big projects, so we may have support for our map needs straight out of the box if we’re already using it in our codebase.
5. Conclusion
In this article, we’ve talked about solutions to have several values for a single key in a map-like structure. We’ve covered three solutions that we can choose from based on our code and needs.
The first solution details every step of the process as we handle manually the values in an ArrayList assigned to each key in the HashMap. It’s a solution compatible with all Java versions, but it’s slightly verbose. Moreover, depending on our implementation, we may run into some computational overhead that newer libraries don’t require.
The second approach can be used to leverage on some newer methods. It still uses a Map with an List to store several values for each key, but we rely on some methods to automate the use of the interface and perform checks.
Finally, the third solution presented looks at interfaces implemented in external libraries, namely MultiValuedMap from Apache Commons Collections, LinkedMultiValueMap from Spring, and Multimap from Google Guava. If we’re already using any of those libraries in our codebase, we can rely on these structures to simplify our code.
















