How to Iterate (Loop) Hashmap in Java With Example

Hashmap can be looped through by using
  • For Loop Iteration using Iterator
  • While Loop iteration with Iterator
  • forEach Loop
  • Stream

Lets understand with following example one by one

1. Explore Hashmap for Loop Iteration with example


/*
 * For Loop Iteration using Iterator
 * 
 */
package javaapplication;

import java.util.Map;

import java.util.HashMap;

import java.util.Iterator;


public class JavaApplication {

  
    public static void main(String[] args) {

        // HashMap object with list of employees with their name and department
        HashMap<String, String> employees = new HashMap<String, String>();

        // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");

        for (Iterator<Map.Entry<String, String>> emp = employees.entrySet().iterator();
                emp.hasNext();) {

            Map.Entry<String, String> data = emp.next();
            System.out.format("key (Name): %s, value (Department): %s %n", data.getKey(), data.getValue());
        }

    }

}

Output once run this code:
key (Name): Joe, value (Department): LEGAL
key (Name): Eric, value (Department): IT
key (Name): John, value (Department): HR

2. Hashmap While Loop traversing using Iterator

package javaapplication;

import java.util.Map;

import java.util.HashMap;

import java.util.Iterator;


public class JavaApplication {


    public static void main(String[] args) {

        // HashMap object with list of employees with their name and department
        HashMap<String, String> employees = new HashMap<String, String>();

        // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");
        
        
        Iterator<Map.Entry<String, String>> emp = employees.entrySet().iterator();
  
        while (emp.hasNext()) {

            Map.Entry<String, String> data = emp.next();
            
            System.out.format("key (Name): %s, value (Department): %s %n", data.getKey(), data.getValue());
        }

    }

}

While Loop code output:

key (Name): Joe, value (Department): LEGAL 
key (Name): Eric, value (Department): IT 
key (Name): John, value (Department): HR 

3. Iterate Hashmap key and values using forEach Loop iteration


package javaapplication;

import java.util.Map;

import java.util.HashMap;


public class JavaApplication {


    public static void main(String[] args) {

        // HashMap object with list of employees with their name and department
        HashMap<String, String> employees = new HashMap<String, String>();

        // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");
        
        
        employees.forEach((name, department) -> {
            System.out.format("(Name): %s, (Department): %s %n", name, department);
        });
    }

}

forEach Loop code output:

key (Name): Joe, value (Department): LEGAL 
key (Name): Eric, value (Department): IT 
key (Name): John, value (Department): HR 

4. Iterate Hashmap key and values using Stream Api



package javaapplication;

import java.util.Map;

import java.util.HashMap;


public class JavaApplication {


    public static void main(String[] args) {

        // HashMap object with list of employees with their name and department
        Map<String, String> employees = new HashMap<>();

        // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");
        
        
      employees.entrySet().stream().forEach(emp -> {
            System.out.format("key (Name): %s, value (Department): %s %n", emp.getKey(), emp.getValue());
        });
    }

}

Stream Loop code output:

key (Name): Joe, value (Department): LEGAL 
key (Name): Eric, value (Department): IT 
key (Name): John, value (Department): HR 

Connect With Us

We hope you find this Post Insightful.

Please Follow Us and Stay tuned with upcoming blog posts and updates.

You May Like to Read

JAVA Articles for Beginners & Advanced Learners