How to Iterate Map in Java

There are several ways to iterate over Map in Java. In this article, we will discuss various ways of iterating map in Java and the best fit of various iteration while implementation.

Iterate Map using for-each loop and KeySet

Using for-each loop, we can iterate over map in Java. This is two step process: first you need to retrieve keySet for the map and then the keySet can be used to iterate using for-each loop. Let us understand how to iterate map using for-each loop and keyset in below code snippet.

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

public final class MapIterationDemo {
	public static void main(String[] args) {
		Map<String, String> mapObjs = new HashMap<String, String>();
		mapObjs.put("JAVA", "java");
		mapObjs.put("MAP", "map");
		mapObjs.put("ITERATOR", "iterator");
		mapObjs.put("FOR-LOOP", "for-loop");
		
		 for (String mapObj : mapObjs.keySet()) {
	            System.out.println("Key: " + mapObj + " -- Value: "+ mapObjs.get(mapObj));
	        }
	}
}
Key: JAVA -- Value: java
Key: ITERATOR -- Value: iterator
Key: FOR-LOOP -- Value: for-loop
Key: MAP -- Value: map

Iterate Map using KeySet Iterator

Map in Java can be traversed using iterator of keyset. First you should get the object of keyset and then using the keyset, you can get the iterator for the same. Now this iterator object can  be traversed using for loop. Let us analyse the traversal of map using keyset iterator.

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

public final class MapIterationDemo {
	public static void main(String[] args) {
		Map<String, String> mapObjs = new HashMap<String, String>();
		mapObjs.put("JAVA", "java");
		mapObjs.put("MAP", "map");
		mapObjs.put("ITERATOR", "iterator");
		mapObjs.put("FOR-LOOP", "for-loop");
		
		Set keySet =  mapObjs.keySet();
		Iterator keySetItr = keySet.iterator();
		while(keySetItr.hasNext()){
			String key = keySetItr.next();
	        System.out.println("Key: " + key + " -- Value: "+ mapObjs.get(key));
	    }
	}
}
Key: JAVA -- Value: java
Key: ITERATOR -- Value: iterator
Key: FOR-LOOP -- Value: for-loop
Key: MAP -- Value: map

Iterate Map using EntrySet and for-each loop

In Java using entryset we can iterate map object. EntrySet is basically collection of all map entries which contains all keys and values. Let us understand the iteration of map using this approach.

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

public final class MapIterationDemo {
	public static void main(String[] args) {
		Map<String, String> mapObjs = new HashMap<String, String>();
		mapObjs.put("JAVA", "java");
		mapObjs.put("MAP", "map");
		mapObjs.put("ITERATOR", "iterator");
		mapObjs.put("FOR-LOOP", "for-loop");
		Set<Entry<String,String>> entrySet =  mapObjs.entrySet();
		for(Entry<String, String> entry : entrySet){
	        System.out.println("Key: " + entry.getKey() + " -- Value: "+  entry.getValue());
	    }
	}
}
Key: JAVA -- Value: java
Key: ITERATOR -- Value: iterator
Key: FOR-LOOP -- Value: for-loop
Key: MAP -- Value: map

Iterate Map using EntrySet and Iterator

Using the iterator, we can iterate map in Java on entryset object obtained from map object in Java. Let us understand the iteration of map using this approach.

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

public final class MapIterationDemo {
	public static void main(String[] args) {
		Map<String, String> mapObjs = new HashMap<String, String>();
		mapObjs.put("JAVA", "java");
		mapObjs.put("MAP", "map");
		mapObjs.put("ITERATOR", "iterator");
		mapObjs.put("FOR-LOOP", "for-loop");
		Set<Entry<String,String>> entrySet =  mapObjs.entrySet();
		Iterator<Entry<String, String>> entrySetItr = entrySet.iterator();
		while (entrySetItr.hasNext()) {
			Entry<String,String> entry = entrySetItr.next();
	        System.out.println("Key: " +entry.getKey() + " -- Value: "+  entry.getValue());
	    }
	}
}
Key: JAVA -- Value: java
Key: ITERATOR -- Value: iterator
Key: FOR-LOOP -- Value: for-loop
Key: MAP -- Value: map

The above article describes how to iterate map in java. In same way you can iterate Hashtable, TreeMap and LinkedHashMap in Java.

Article tagged as
Author
Author: Amit Gupta
Published On: 09/09/2015
Last revised On: 13/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media