Java 8 – How to store multiple values for single key in HashMap ?

In this article, we will discuss how to store/put multiple values for single key in HashMap

Sometimes, we might require to store multiple values for its corresponding key in Map like storing all/few top countries belonging to particular continent

  • Asian continent – India, China, Japan, Mongolia
  • African continent – Libya, Mauritius, Gambia, Kenya
  • European continent – Russia, France, Moldova, Italy

1. HashMap – Single key with Multiple values

  • In this illustration, we are storing couple of top countries of Asian, African and European continents
  • Create HashMap object with Key being String type and Value as List of Strings
    • In the 1st list, we are storing countries belonging to Asian continent and putting into HashMap as Value with Key as “Asian
    • For 2nd list, we are storing countries belonging to African continent and putting into HashMap as Value with Key as “African
    • For 3rd list, we are storing countries belonging to Euorpean continent and putting into HashMap as Value with Key as “European
  • Finally, iterate and print to console using Java 8 forEach() method of Map class

HashMapWithSingleKeyAndMultipleValues.java

package in.bench.resources.iterating.hashmap.of.arraylist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HashMapWithSingleKeyAndMultipleValues {

	public static void main(String[] args) {

		// 1. create HashMap
		Map<String, List<String>> topContriesOfContinent =
				new HashMap<String, List<String>>();


		// 1.1 create List 1
		List<String> asianContinentCountries =
				new ArrayList<String>();


		// 1.1.1 add top countries of Asian continent
		asianContinentCountries.add("India");
		asianContinentCountries.add("China");
		asianContinentCountries.add("Japan");
		asianContinentCountries.add("Mongolia");


		// 1.2 create List 2
		List<String> africanContinentCountries =
				new ArrayList<String>();


		// 1.2.1 add top countries of African continent
		africanContinentCountries.add("Libya");
		africanContinentCountries.add("Mauritius");
		africanContinentCountries.add("Gambia");
		africanContinentCountries.add("Kenya");


		// 1.3 create List 3
		List<String> europeanContinentCountries =
				new ArrayList<String>();


		// 1.3.1 add top countries of African continent
		europeanContinentCountries.add("Russia");
		europeanContinentCountries.add("France");
		europeanContinentCountries.add("Moldova");
		europeanContinentCountries.add("Italy");


		// 1.4 put list to Map
		topContriesOfContinent.put("Asian", asianContinentCountries);
		topContriesOfContinent.put("African", africanContinentCountries);
		topContriesOfContinent.put("European", europeanContinentCountries);



		// 1.5 iterate and print to console
		System.out.println("HashMap with Single Key and Multiple Values : \n");
		topContriesOfContinent.forEach(
				(key,value) -> System.out.println("Key (Continent) : " + key 
						+ "\n\t Values (Countries) : " + value)
				);
	}
}

Output:

HashMap with Single Key and Multiple Values : 

Key (Continent) : European
	 Values (Countries) : [Russia, France, Moldova, Italy]
Key (Continent) : Asian
	 Values (Countries) : [India, China, Japan, Mongolia]
Key (Continent) : African
	 Values (Countries) : [Libya, Mauritius, Gambia, Kenya]

2. HashMap – Filter from Multiple values for a Key

  • This is the same example we seen in the previous section, but here we are going to filter values on the basis of some condition
  • For each of the continents, we are going to filter those countries which are starting with capital letter “M
  • Finally, iterate and print to console using Java 8 forEach() method of Map class

FilterFromMultipleValuesForKey.java

package in.bench.resources.iterating.hashmap.of.arraylist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FilterFromMultipleValuesForKey {

	public static void main(String[] args) {

		// 1. create HashMap
		Map<String, List<String>> topContriesOfContinent =
				new HashMap<String, List<String>>();


		// 1.1 create List 1
		List<String> asianContinentCountries =
				new ArrayList<String>();


		// 1.1.1 add top countries of Asian continent
		asianContinentCountries.add("India");
		asianContinentCountries.add("China");
		asianContinentCountries.add("Japan");
		asianContinentCountries.add("Mongolia");


		// 1.2 create List 2
		List<String> africanContinentCountries =
				new ArrayList<String>();


		// 1.2.1 add top countries of African continent
		africanContinentCountries.add("Libya");
		africanContinentCountries.add("Mauritius");
		africanContinentCountries.add("Gambia");
		africanContinentCountries.add("Kenya");


		// 1.3 create List 3
		List<String> europeanContinentCountries =
				new ArrayList<String>();


		// 1.3.1 add top countries of African continent
		europeanContinentCountries.add("Russia");
		europeanContinentCountries.add("France");
		europeanContinentCountries.add("Moldova");
		europeanContinentCountries.add("Italy");


		// 1.4 put list to Map
		topContriesOfContinent.put("Asian", asianContinentCountries);
		topContriesOfContinent.put("African", africanContinentCountries);
		topContriesOfContinent.put("European", europeanContinentCountries);


		// 1.5 filter those all values staring with "M" for each Key 
		Map<String, List<String>> filteredCountries = topContriesOfContinent
				.entrySet()
				.stream()
				.collect(Collectors.toMap(Map.Entry::getKey, 
						value -> value.getValue().stream().filter(val -> val.startsWith("M"))
						.collect(Collectors.toList())));


		// 1.6 iterate and print to console
		System.out.println("HashMap - Filter from Multiple Values for a Key : \n");
		filteredCountries
		.entrySet()
		.forEach(entry -> System.out.println("Key (Continent) : " + entry.getKey() 
		+ "\n\t Values (Countries) : " + entry.getValue()));
	}
}

Output:

HashMap - Filter from Multiple Values for a Key : 

Key (Continent) : European
	 Values (Countries) : [Moldova]
Key (Continent) : Asian
	 Values (Countries) : [Mongolia]
Key (Continent) : African
	 Values (Countries) : [Mauritius]

Related Articles:

References :

Happy Coding !!
Happy Learning !!

Java 8 - Find sum of Largest 2 numbers in an Array or List or Stream ?
Java 8 - How to find duplicate in a Stream or List ?