How to Add Multiple Values for Single Key In HashMap in Java

This article discusses the HashMap in Java and how to add multiple values for Single Key In HashMap in Java.

HashMap

The HashMap in Java is an instrument that allows efficient access, insertion, deletion, and updating using the indexing concept by storing data in the form of key and value pairs.

It was introduced in the Java 1.2 version and hence has been a part of the Java Collection library.

It is included in java.util package. Java 5 denotes HashMaps as HashMap where K stands for the Key and V stands for the Value.

Can HashMap Store Multiple Values Automatically

HashMap shares similarities with HashTable, apart from the fact that it is asynchronous. It implies that HashMap can store null keys which HashTable cannot.

HashMap allows storage for any number of Null Values, but there has to be only one Null Key in the whole Map.

Another important property of HashMap is that if it encounters a different value for an already existing key, it overwrites the value for the same key.

This is what can sometimes be not wanted for a programming use case. And thus in this article, we will discuss a workaround for storing multiple values to the same key in HashMap in Java.

Ways to Add Multiple Values for Single Key In HashMap in Java

Using the Standard Library

If you are required to add different values under the same key, one of the methods could be to use a Java collection class such as an ArrayList, List, etc.

There are many such collections classes in Java that you can embed inside the HashMap, this would enable the whole collection to come under the same key in the HashMap.

The following sample code uses the ArrayList from java.util library. It is used to store the different values which later become a part of the HashMap.

Let us look at the following Java code for a better understanding of how it works.

Output :

4=[1+3, 2+2, 3+1] 5=[1+4, 2+3, 3+2, 4+1]

In the above code, different combinations that make up a number are put under the same key in the HashMap.

The HashMap uses the Integer class and the ArrayList of Strings. The Strings are different combinations, for example, the string “1+3” results in 4, which is the key value for that string.

Using Apache Commons Library

Apache Commons library extends the Java API, it is a complete package with a very useful set of utility classes.
You need to add below dependency to get Apache commons jar.

It has implementations on many ordered data structures and manipulations of basic data structures.

You can use the MultiMap and MultiValueMap available in the Apache Commons collections library to store multiple values in the same key.

The following code illustrates the same.

Output:

4=[1+3, 2+2, 3+1] 5=[1+4, 2+3, 3+2, 4+1]

The above code implements the same example of storing combinations of numbers is with the output of the combinations stored as keys in the Hash Map.

However, the implementation is using the Apache Commons library that you can add as an external jar file or as dependency in the pom file of maven project.

Using Google Guava Library

Google guava is another similar library built by google, it was primarily developed for java projects within google, but it was hosted as an open source library.

You can use the Google Guava Library on any maven project similar to the Apache Commons library.
Here is the dependency which you need to add for guava in pom.xml.

This example uses the ArrayListMultiMap from the google guava library.

Similar to the Apache Commons library you can add this library as dependency in the pom file of maven project or as an external jar file library.

Output :

4=[1+3, 2+2, 3+1] 5=[1+4, 2+3, 3+2, 4+1]

This code imports the ArrayListMultimapfrom google guava and uses for making the multi map which again stores the combinations in the same output key in the hash map.

Using TreeSet as Values

TreeSet is a very useful data set as it implements the navigable set which is derived from the sorted set which is again derived from the set class of the java library.

It comes handy when there is a requirement to store a huge amount of data but access to it has to be fast.

Note that you cannot insert null value into TreeSet as it cannot be compared to any other already existing data in the TreeSet.

We will continue with the above example of implementing storage of combinations of numbers in the same key of a Hash Map.

The following example code explains how TreeSet can be embedded into the HashMap in Java.

Output :

4=[1+3, 2+2, 3+1] 5=[1+4, 2+3, 3+2, 4+1]

As you can observe, the above code prints the output in the sorted order when implemented with the TreeSet embedded into the HashMap.

Access to HashMap values may be quicker but as you can see it may require extra usage of memory because lists are separately initialized.

Using a Wrapper Class

Wrapper Class in general in Java refers to a class which converts a primitive data type into objects of a class or from objects of a class to a primitive data type.

We have used the Integer Class in previous example codes which is an example of a wrapper class which wraps the primitive data type ‘int’.

We can also use Wrapper Class for an array or List of Objects.

To understand how wrapper class can help in getting multiple values under the same key in HashMap, let us look at the following code.

Output:

4=[3+1] [2+2] [1+3] 5=[4+1] [3+2] [2+3] [1+4]

In the above sample code, Wrapper Class wraps an array of Strings which is a private member of the class.

The HashMap declared in the main class thus only has to implement an Integer for the key and for values, an object of the “Wrapped” class, allowing us to store multiple values under the same key.

Using Java Tuples

Tuples in Java provides another solution for combining elements.

A tuple is considered to be an ordered data structure that can contain different data types, these might not be connected to one another but as a whole has a significant meaning.

A noteworthy point about Tuples is that they are immutable, i.e once created they cannot be changed.

The Tuples Library in Java consists of many useful classes such as the Unit, Pairs, Triplets, Quartets, etc.
You need to add below dependency to get javatuples jar.

To understand how it can be used for storing multiple values under the same key in Hash Map in Java, let us look at the following code.

Output :

4=[1+3, 2+2, 3+1]

This sample code uses the Triplet Class of Java from the JavaTuples Library.

This code creates the Hash Map of Integer and Triplet. While populating the hash map the code creates an object of the Triplet class as shown in the code.

Using compute() Function in JDK 8

If you are using a JDK version 8 or newer, you can make use of the compute() function as well.
The map.compute() function is a remapping function that allows the manipulation of the values inside the Hash Maps of Java.

Another point to note while using compute functions is to make sure that it does not alter the existing map.

Thus, this compute function can be used to manipulate the values such that multiple values are added to the same key.

The compute() function is generally used for updating values based on specific conditions. However, we can make simple changes into its embedding so as to make it useful in our use case.

To understand how this concept works, let us look at the following piece of code.

Output:

4=[1+3, 2+2, 3+1] 5=[1+4, 2+3, 3+2, 4+1]

On closer observation, you can see that the code explicitly modifies the put() function and uses it for insertion into the Hash Map.

The code declares the put() function as private static and it takes the three parameters as Map, Key, and Value.

Inside the function, the code uses the map.compute() function that creates an ArrayList if not already existing and if it does, adds the new value into it.

Conclusion

These are the different ways that you can use when there is a need to add multiple values into the same key of a Hash Map in Java.

The easiest way is to use the standard library data structures such as the ArrayList , etc.

However, if you are working on a maven project it could be easier to use MultiMap from external libraries such as google guava and Apache Commons as discussed in the article.

This article also discussed other workarounds like Java Tuples, Wrapper Class, the compute() function, and TreeSet as Values, for the problem, which may not be easy to implement but can be efficient depending upon the use case.

This is all about how to add multiple values for Single Key in HashMap in Java

Hope you have learned something new and enjoyed reading the article. Stay tuned for more such articles. Happy learning!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *