How to iterate over Map or HashMap in java

In this post we will see different ways we can iterate through a map , HashMap and TreeMAp in Java. There are following ways for iterating map in java.

  • Using EntrySet() and for each loop – In this way we Iterate Map Entries (Keys and Values) with the help for each loop
  • Using keyset() and for each loop – Here we  Iterate Map Keys Only with the help of for each loop
  • Using EntrySet() and java Iterator – In this way we Iterate Map Entries (Keys and Values) with the help Iterator
  • Using keyset() and java Iterator – Here we  Iterate Map Keys Only with the help of Iterator
  • Iterate through values of a hashmap – In this way we will see how to iterate through values of a hashmap.
package com.demo;

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

public class HashMapExample {
	
	public static void main(String[] args) {
		
		/*
		 *Constructs an empty HashMap with the default initial capacity (16) 
		 *and the default load factor (0.75).
		 */
		Map<String, String> hashMap = new HashMap<String, String>();
		
		hashMap.put("hcl", "amit");
		hashMap.put("tcs","ravi");
		hashMap.put("wipro","anmol");
		

  System.out.println("Iterating  HashMap using entrySet with simple for-each loop ===>>");
        for(Map.Entry<String, String> entry: hashMap.entrySet()) {
            System.out.println("Key Of map = "+ entry.getKey() + 
                             " , value of map = " + entry.getValue() );
        }
 
    System.out.println("Iterating  HashMap using keySet with simple for-each loop ===>>");
        for(String key : hashMap.keySet()) {
            System.out.println("Key Of map = "+ key + " , 
                             value of map = " + hashMap.get(key) );
        }
         
 System.out.println("====>> Iterating  HashMap using entrySet ===>>");
        Set<Map.Entry<String, String>> employeeEntries = hashMap.entrySet();
        Iterator<Map.Entry<String, String>> employeeIterator = employeeEntries.iterator();
        while (employeeIterator.hasNext()) {
         Map.Entry<String, String> entry = employeeIterator.next();
System.out.println("Key Of map = "+ entry.getKey() + " ,
                                   value of map = " + entry.getValue() );
          }

		System.out.println("====>> Iterating  HashMap using keySet ===>>");
        Set<String> keySet = hashMap.keySet();
        Iterator<String> keysIterator = keySet.iterator();
        while (keysIterator.hasNext()) {
            String key = keysIterator.next();
     System.out.println("Key Of map = "+ key + " , value of map = " + hashMap.get(key) );
          }
  System.out.println("Iterating  HashMap using values with simple for-each loop ===>>");
        for(String value : hashMap.values()) {
            System.out.println("Value Of map = "+ value );
        }
       
        
	}

}

Output:

Iterating  HashMap using entrySet with simple for-each loop ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
Iterating  HashMap using keySet with simple for-each loop ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
====>> Iterating  HashMap using entrySet ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
====>> Iterating  HashMap using keySet ===>>
Key Of map = hcl , value of map = amit
Key Of map = tcs , value of map = ravi
Key Of map = wipro , value of map = anmol
Iterating  HashMap using values with simple for-each loop ===>>
Value Of map = amit
Value Of map = ravi
Value Of map = anmol

Reference :

A Guide To HashMap                                                                                                            A Guide To ArrayList                                                                                                            Java Doc – HashSet                                                                                                              Wikipedia Link                                                                                                                    Java Collection

Leave a Reply

Your email address will not be published. Required fields are marked *

− 2 = 2