Java – Different ways to iterate over HashMap of ArrayList

In one of the previous articles, we already discussed different ways to iterate through Map but those are with String object only i.e.; both keys and values are in String-type only

Q) What if we want to iterate through HashMap of ArrayList ?

  • where keys are in either String/Integer type
  • values are ArrayList of String type
  • or some other type of our interest like Double, Integer or Float, etc.

1. Different ways to iterate through Map :

2. Different ways to iterate over HashMap of ArrayList

We will limit our code to 3 demo examples i.e.,

  • Using keySet(); and enhanced for-each loop
  • Using entrySet(); and Iterator interface
  • Using forEach in Java 1.8 version

Let us move forward and discuss all possible ways to iterate HashMap of ArrayList of (String) type

2.1 Get Map.keySet() and Iterate using enhanced for-loop

IteratingHashMapUsingKeySetAndForLoop.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.Set;

public class IteratingHashMapUsingKeySetAndForLoop {

	public static void main(String[] args) {

		// create HashMap of Continent & list of Top Countries
		Map<String, List<String>> continentTopContries =
				new HashMap<String, List<String>>();

		// create ArrayList-1
		// for adding top countries of Asian Continent
		List<String> topCountriesOfAsiaContinent =
				new ArrayList<String>();

		// add top countries of Asian continent
		topCountriesOfAsiaContinent.add("India");
		topCountriesOfAsiaContinent.add("China");
		topCountriesOfAsiaContinent.add("Russia");
		topCountriesOfAsiaContinent.add("Japan");

		// put 1st entry as Asia and its list of Top countries
		continentTopContries.put("Asia",
				topCountriesOfAsiaContinent);

		// create ArrayList-2
		// for adding top countries of Africa Continent
		List<String> topCountriesOfAfricaContinent =
				new ArrayList<String>();

		// add top countries of African continent
		topCountriesOfAfricaContinent.add("Libya");
		topCountriesOfAfricaContinent.add("Zimbabwe");
		topCountriesOfAfricaContinent.add("Nigeria");
		topCountriesOfAfricaContinent.add("Kenya");

		// put 2nd entry as Africa and its list of Top countries
		continentTopContries.put("Africa",
				topCountriesOfAfricaContinent);

		// Way 1: Get keySet() and
		// Iterate using for-each loop for ArrayList
		System.out.println("Way 1: Get keySet()"
				+ " and Iterate using for-each loop");

		// get keySet() into Set
		Set<String> setOfKeySet = continentTopContries.keySet();

		// for-each loop
		for(String key : setOfKeySet) {

			System.out.println("\nContinent name : "  + key
					+ "\nList of Top Countries of " + key + ":");

			for(String country : continentTopContries.get(key)) {
				System.out.println("\t\t\t\t" + country);
			}
		}
	}
}

Output:

Way 1: Get keySet() and Iterate using for-each loop

Continent name : Asia
List of Top Countries of Asia:
				India
				China
				Russia
				Japan

Continent name : Africa
List of Top Countries of Africa:
				Libya
				Zimbabwe
				Nigeria
				Kenya

2.2 Get Map.entrySet() and Iterate using Iterator interface

IteratingHashMapUsingEntrySetAndIterator.java

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class IteratingHashMapUsingEntrySetAndIterator {

	public static void main(String[] args) {

		// create HashMap of Continent and list of Top Countries
		Map<String, List<String>> continentTopContries =
				new HashMap<String, List<String>>();

		// create ArrayList-1
		// for adding top countries of Asian Continent
		List<String> topCountriesOfAsiaContinent =
				new ArrayList<String>();

		// add top countries of Asian continent
		topCountriesOfAsiaContinent.add("India");
		topCountriesOfAsiaContinent.add("China");
		topCountriesOfAsiaContinent.add("Russia");
		topCountriesOfAsiaContinent.add("Japan");

		// put 1st entry as Asia and its list of Top countries
		continentTopContries.put("Asia",
				topCountriesOfAsiaContinent);

		// create ArrayList-2
		// for adding top countries of Africa Continent
		List<String> topCountriesOfAfricaContinent =
				new ArrayList<String>();

		// add top countries of African continent
		topCountriesOfAfricaContinent.add("Libya");
		topCountriesOfAfricaContinent.add("Zimbabwe");
		topCountriesOfAfricaContinent.add("Nigeria");
		topCountriesOfAfricaContinent.add("Kenya");

		// put 2nd entry as Africa and its list of Top countries
		continentTopContries.put("Africa",
				topCountriesOfAfricaContinent);

		// Way 2: Get entrySet() and
		// Iterate using Iterator interface for ArrayList
		System.out.println("Way 2: Get entrySet() and  Iterate "
				+ "using Iterator interface for ArrayList");

		// get entrySet() into Set
		Set<Map.Entry<String, List<String>>> entrySet =
				continentTopContries.entrySet();

		// Collection Iterator
		Iterator<Entry<String, List<String>>> iterator =
				entrySet.iterator();

		while(iterator.hasNext()) {

			Entry<String, List<String>> entry = iterator.next();

			System.out.println("\nContinent name : "
					+ entry.getKey()
					+ "\nList of Top Countries of "
					+ entry.getKey() + ":");

			for(String country : entry.getValue()) {
				System.out.println("\t\t\t\t" + country);
			}
		}
	}
}

Output:

Way 2: Get entrySet() and  Iterate using Iterator interface for ArrayList

Continent name : Asia
List of Top Countries of Asia:
				India
				China
				Russia
				Japan

Continent name : Africa
List of Top Countries of Africa:
				Libya
				Zimbabwe
				Nigeria
				Kenya

Note:

  • keySet(); and entrySet(); methods of Map from Java 1.2 version
  • Iterator interface for iterating comes from Java 1.2 version
  • Whereas enhanced for-each loop introduced in Java 1.5 version

2.3 Using Java 8’s Iterable.forEach() method

IteratingHashMapUsingJava8ForEach.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 IteratingHashMapUsingJava8ForEach {

	public static void main(String[] args) {

		// create HashMap of Continent and list of Top Countries
		Map<String, List<String>> continentTopContries =
				new HashMap<String, List<String>>();

		// create ArrayList-1
		// for adding top countries of Asian Continent
		List<String> topCountriesOfAsiaContinent =
				new ArrayList<String>();

		// add top countries of Asian continent
		topCountriesOfAsiaContinent.add("India");
		topCountriesOfAsiaContinent.add("China");
		topCountriesOfAsiaContinent.add("Russia");
		topCountriesOfAsiaContinent.add("Japan");

		// put 1st entry as Asia and its list of Top countries
		continentTopContries.put("Asia",
				topCountriesOfAsiaContinent);

		// create ArrayList-2
		// for adding top countries of Africa Continent
		List<String> topCountriesOfAfricaContinent =
				new ArrayList<String>();

		// add top countries of African continent
		topCountriesOfAfricaContinent.add("Libya");
		topCountriesOfAfricaContinent.add("Zimbabwe");
		topCountriesOfAfricaContinent.add("Nigeria");
		topCountriesOfAfricaContinent.add("Kenya");

		// put 2nd entry as Africa and its list of Top countries
		continentTopContries.put("Africa",
				topCountriesOfAfricaContinent);

		// Way 3: Using Iterable.forEach() in Java 8 version
		System.out.println("Way 3: Using Iterable.forEach()"
				+ " in Java 8 version\n");

		// Iterating Map/Hashtable using forEach() in Java 8
		continentTopContries.forEach(
				(key, value)->System.out.println(
						"Continent name : " + key + "\t\t"
								+ "List of Top Countries : " + value));
	}
}

Output:

Way 3: Using Iterable.forEach() in Java 8 version

Continent name : Asia		List of Top Countries : [India, China, Russia, Japan]
Continent name : Africa		List of Top Countries : [Libya, Zimbabwe, Nigeria, Kenya]

From above example, HashMap

  • allows only unique keys to be stored
  • null object is allowed; but maximum of one
  • while iterating, keys are retrieved in random-order

Related Articles:

Happy Coding !!
Happy Learning !!

Java - Sorting list of objects on multiple fields using Comparator
Java - Iterate through Hashtable in 6 ways