Java 8 – Iterating HashMap in 8 ways

In this article, we will learn different ways to iterate through HashMap

Different ways to iterate through Map :

  1. Using Map.forEach() method
  2. Using Map.keySet() and Stream.forEach() methods
  3. Using Map.entrySet() and Stream.forEach() methods
  4. Using Map.keySet() method and enhanced for-loop
  5. Using Map.entrySet() method and enhanced for-loop
  6. Using Map.keySet() method and Iterator interface
  7. Using Map.entrySet() method and Iterator interface
  8. Using Enumeration interface and while-loop

1. Using Map.forEach() method

IterateMapUsingForEach.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingForEach {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.forEach : \n");


		// 1.3 iterate and print to console using Map.forEach
		rankCountry.forEach((rank, county) -> {
			System.out.println("Key : " + rank + "\t Value : " + county);
		});
	}
}

Output:

Java 8 - using Map.forEach : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

2. Using Map.keySet() and Stream.forEach() methods

IterateMapUsingKeySetAndStreamForEach.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingKeySetAndStreamForEach {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.keySet() and Stream.forEach : \n");


		// 1.3 iterate and print to console using Map.keySet() and Stream.forEach
		rankCountry
		.keySet()
		.stream()
		.forEach(rank -> {
			System.out.println("Key : " + rank + "\t Value : " + rankCountry.get(rank));
		});
	}
}

Output:

Java 8 - using Map.keySet() and Stream.forEach : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

3. Using Map.entrySet() and Stream.forEach() methods

IterateMapUsingEntrySetAndStreamForEach.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingEntrySetAndStreamForEach {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.entrySet() and Stream.forEach : \n");


		// 1.3 iterate and print to console using Map.entrySet() and Stream.forEach
		rankCountry
		.entrySet()
		.stream()
		.forEach(entry -> {
			System.out.println(
					"Key : " + entry.getKey() + "\t Value : " + entry.getValue()
					);
		});
	}
}

Output:

Java 8 - using Map.entrySet() and Stream.forEach : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

4. Using Map.keySet() and enhanced for-loop

class.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingKeySetAndForLoop {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.keySet() and for-loop : \n");


		// 1.3 iterate and print to console using Map.keySet() and for-loop
		for(Integer rank : rankCountry.keySet()) {

			System.out.println("Key : " + rank + "\t Value : " + rankCountry.get(rank));
		}
	}
}

Output:

Java 8 - using Map.keySet() and for-loop : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

5. Using Map.entrySet() and enhanced for-loop

IterateMapUsingEntrySetAndForLoop.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingEntrySetAndForLoop {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.entrySet() and for-loop : \n");


		// 1.3 iterate and print to console using Map.entrySet() and for-loop
		for(Map.Entry<Integer, String> entry : rankCountry.entrySet()) {

			System.out.println(
					"Key : " + entry.getKey() + "\t Value : " + entry.getValue()
					);
		}
	}
}

Output:

Java 8 - using Map.entrySet() and for-loop : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

6. Using Map.keySet() and Iterator interface

IterateMapUsingKeySetAndIterator.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class IterateMapUsingKeySetAndIterator {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.keySet() and Iterator : \n");


		// 1.3 get iterator from Map
		Iterator<Integer> iterator = rankCountry.keySet().iterator();


		// 1.4 iterate and print to console using Map.keySet() and Iterator
		while (iterator.hasNext()) {

			Integer rank = iterator.next();

			System.out.println("Key : " + rank + "\t Value : " + rankCountry.get(rank));
		}
	}
}

Output:

Java 8 - using Map.keySet() and Iterator : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

7. Using Map.entrySet() and Iterator interface

IterateMapUsingEntrySetAndIterator.java

package net.bench.resources.ways.to.iterate.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class IterateMapUsingEntrySetAndIterator {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java 8 - using Map.entrySet() and Iterator : \n");


		// 1.3 get iterator
		Iterator<Map.Entry<Integer, String>> itr = rankCountry.entrySet().iterator();


		// 1.4 iterate and print to console using Map.entrySet() and Iterator
		while (itr.hasNext()) {

			Map.Entry<Integer, String> entry = itr.next();

			System.out.println(
					"Key : " + entry.getKey() + "\t Value : " + entry.getValue()
					);
		}
	}
}

Output:

Java 8 - using Map.entrySet() and Iterator : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

8. Using Enumeration interface and while-loop

IterateMapUsingEnumeration.java

package net.bench.resources.ways.to.iterate.map;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class IterateMapUsingEnumeration {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> rankCountry = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		rankCountry.put(1, "India");
		rankCountry.put(2, "Japan");
		rankCountry.put(3, "China");
		rankCountry.put(4, "Oman");
		rankCountry.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Java - using Enumeration : \n");


		// 1.3 get enumeration of Keys
		final Enumeration<Integer> e = Collections.enumeration(rankCountry.keySet());


		// 1.4 iterate using while-loop and print to console
		while(e.hasMoreElements()) {      

			// 1.4.1 get key
			Integer key = e.nextElement();

			// 1.4.2 get value based on key
			String value = rankCountry.get(key);

			// 1.4.3 print Key-Value pairs to console
			System.out.println("Key : " + key + "\t Value : " + value);
		}
	}
}

Output:

Java - using Enumeration : 

Key : 1	 Value : India
Key : 2	 Value : Japan
Key : 3	 Value : China
Key : 4	 Value : Oman
Key : 5	 Value : Qatar

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert HashMap to ArrayList ?
Java 8 - Comparator.thenComparingDouble() method