Various ways to iterate over HashMap of ArrayList in Java

In previous articles, we have discussed various ways to iterate through Map but those are with String object only i.e.; both keys and values are in String type only

 

What if we want to itreate through HashMap of ArrayList like

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

 

Various ways to iterate through Map

  • Using keySet()method and for-each loop
  • Using keySet()method and Iterator interface
  • Using entrySet()method and for-each loop
  • Using entrySet()method and Iterator interface
  • Using forEach() in Java 8

Read different ways to iterate Map Entry

Read how to iterate Map using Iterable.forEach() in Java 8

 

Various ways to iterate over HashMap of ArrayList in Java

But we will limit our code to three demo examples i.e.,

  • Using keySet and enhanced for-loop
  • Using entrySet and Iterator interface
  • Using forEach in Java 8

 

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

Way 1: Get keys using keySet() method of Map 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 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

 

Way 2: Get entries using entrySet() method of Map 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 and Iterator interface for iterating comes from Java 1.2 version, whereas enhanced for loop introduced in Java 1.5 version

 

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

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

 

Happy Coding !!
Happy Learning !!

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