Java – Iterate through HashMap in 5 ways

In this article, we will discuss various ways to iterate through HashMap or implementation classes of Map interface like LinkedHashMap or TreeMap

Map implementation classes :

  • HashMap –> iterate through entries or Key-Value pairs, in random-order
  • LinkedHashMap –> iterate through entries or Key-Value pairs, as per insertion-order
  • TreeMap –> iterate through entries or Key-Value pairs, as per some sorting-order

Various ways to iterate through HashMap:

  1. Using keySet() method and for-each loop
  2. Using keySet() method and Iterator interface
  3. Using entrySet() method and for-each loop
  4. Using entrySet() method and Iterator interface

DifferentWaysToIterateHashMap.java

package in.bench.resources.java.map;

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

public class DifferentWaysToIterateHashMap {

	public static void main(String[] args) {

		// creating HashMap object of type <String, String>
		HashMap<String, String> hm = new HashMap<String, String>(); 

		// adding key-value pairs to HashMap object
		hm.put("Google", "Sundar Pichai");
		hm.put("Facebook", "Mark Zuckerberg");
		hm.put("LinkedIn", "Reid Hoffman");
		hm.put("Apple", "Steve Jobs");
		hm.put("Microsoft", "Bill Gates");

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

		// getting keySet() into Set
		Set<String> set1 = hm.keySet();

		// for-each loop
		for(String key : set1) {
			System.out.println("Key : "  + key + "\t\t"
					+ "Value : "  + hm.get(key));
		}

		// Way 2: Get keySet() and Iterate using Iterator interface
		System.out.println("\n\nWay 2: Get keySet() and "
				+ "Iterate using Iterator interface\n");

		// getting keySet() into Set
		Set<String> set2 = hm.keySet();

		// Collection Iterator
		Iterator<String> itr1 = set2.iterator();

		while(itr1.hasNext()) {
			String key = itr1.next();
			System.out.println("Key : "  + key + "\t\t"
					+ "Value : "  + hm.get(key));
		}

		// Way 3: Get entrySet() and Iterate using for-each loop
		System.out.println("\n\nWay 3: Get entrySet() and "
				+ "Iterate using for-each loop\n");

		// getting entrySet() into Set
		Set<Entry>String, String>> entrySet1 = hm.entrySet();

		// for-each loop
		for(Entry<String, String> entry1 : entrySet1) {
			System.out.println("Key : "  + entry1.getKey() + "\t\t"
					+ "Value : "  + entry1.getValue());
		}

		// Way 4: Get entrySet() and Iterate using Iterator interface
		System.out.println("\n\nWay 4: Get entrySet() and "
				+ "Iterate using Iterator interface\n");

		// getting entrySet() into Set
		Set<Entry>String, String>> entrySet2 = hm.entrySet();

		// Collection Iterator
		Iterator<Entry<String, String>> itr2 = entrySet2.iterator();

		while(itr2.hasNext()) {
			Entry<String, String> entry2 = itr2.next();
			System.out.println("Key : "  + entry2.getKey() + "\t\t"
					+ "Value : "  + entry2.getValue());
		}
	}
}

Output:

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

Key : Google		Value : Sundar Pichai
Key : Apple		Value : Steve Jobs
Key : Microsoft		Value : Bill Gates
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg

Way 2: Get keySet() and Iterate using Iterator interface

Key : Google		Value : Sundar Pichai
Key : Apple		Value : Steve Jobs
Key : Microsoft		Value : Bill Gates
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg

Way 3: Get entrySet() and Iterate using for-each loop

Key : Google		Value : Sundar Pichai
Key : Apple		Value : Steve Jobs
Key : Microsoft		Value : Bill Gates
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg

Way 4: Get entrySet() and Iterate using Iterator interface

Key : Google		Value : Sundar Pichai
Key : Apple		Value : Steve Jobs
Key : Microsoft		Value : Bill Gates
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg

In the next article, we will see a demo example on how to iterate Map in Java 1.8 i.e.;

Related Articles:

To conclude, now there are 5 ways to iterate Map Entries

References:

Happy Coding !!
Happy Learning !!

Java 8 - Iterating Map using forEach() method
Java - How to get size or length of HashMap ?