Java : Creating HashMap by associating multiple values with same Key

In this article we will discuss how to create a HashMap with multiple values associated with same Key in Java.

Suppose we want to create a HashMap to keep the track of strings and their occurrences in text. For example.

“the” occurred at following indexes 1,4,8,12.
“at” occurred at following indexes 22, 28, 44 ,89
“from” occurred at following indexes 32, 39, 51, 73

To store this data in a lookup table / map , we need to create a HashMap with key as string and then associate multiple values with same key. We can do this by storing a List of Integers i.e. List<Integer> objects as values in HashMap.

HashMap with List<T> Object as Value

Let’s Create a HashMap of String as Key and List<Integer> as value i.e.

// Create a HashMap of key as String List Of integers as Value
HashMap<String, List<Integer>> wordFreqMap = new HashMap<>();

Now let’s push elements into this HashMap,

// Create an array Of Occurrences
Integer[] occurences = { 12, 8, 4, 1 };

// Add List as value in the map
wordFreqMap.put("the", Arrays.asList(occurences));

// Add List as value in the map
wordFreqMap.put("at", Arrays.asList(new Integer[] { 28, 44, 22, 89 }));

Complete example is as follows,

package com.thispointer.examples.java.collection.hashmap;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MultipleValuesListExample {

	public static void main(String[] args) {

		// Create a HashMap of key as String List Of integers as Value
		HashMap<String, List<Integer>> wordFreqMap = new HashMap<>();

		// Create an array Of Occurrences
		Integer[] occurences = { 12, 8, 4, 1 };

		// Add List as value in the map
		wordFreqMap.put("the", Arrays.asList(occurences));

		// Add List as value in the map
		wordFreqMap.put("at", Arrays.asList(new Integer[] { 28, 44, 22, 89 }));

		// Add List as value in the map
		wordFreqMap.put("from", Arrays.asList(new Integer[] { 39, 32, 73, 51 }));

		// Print the Map Contents
		System.out.println(wordFreqMap);

	}

}

Output:

{the=[12, 8, 4, 1], at=[28, 44, 22, 89], from=[39, 32, 73, 51]}

Idea behind associating Multiple values with same key is to store another Collection as Value in the HashMap. This can be a List or Set or any other Map too.

HashMap with TreeSet as Value

We can create a HashMap with TreeSet as Value too i.e.

// Create a HashMap of key as String List Of integers as Value
HashMap<String, TreeSet<Integer>> wordFreqMap = new HashMap<>();

Let’s change the above example and associate keys with TreeSet of Integers instead of List. It will store the Integers in Sorted Order. Checkout complete example as follows,

package com.thispointer.examples.java.collection.hashmap;

import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;

public class MultipleValuesSetExample {

	public static void main(String[] args) {

		// Create a HashMap of key as String List Of integers as Value
		HashMap<String, TreeSet<Integer>> wordFreqMap = new HashMap<>();

		// Create an array Of Occurrences
		Integer[] occurences = { 12, 8, 4, 1 };

		// Add TreeSet as value in the map
		wordFreqMap.put("the", new TreeSet<Integer>(Arrays.asList(occurences)));

		// Add TreeSet as value in the map
		wordFreqMap.put("at", new TreeSet<Integer>(Arrays.asList(new Integer[] { 28, 44, 22, 89 })));

		// Add TreeSet as value in the map
		wordFreqMap.put("from", new TreeSet<Integer>(Arrays.asList(new Integer[] { 39, 32, 73, 51 })));

		// Print the Map Contents
		System.out.println(wordFreqMap);

	}

}

Output:

{the=[1, 4, 8, 12], at=[22, 28, 44, 89], from=[32, 39, 51, 73]}

 

 

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top