Java Program to Iterate over a HashMap

To understand this example, you should have the knowledge of the following Java programming topics:


In Java HashMap, we can iterate through its keys, values, and key/value mappings.

Example 1: Iterate through HashMap using the forEach loop

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

class Main {
  public static void main(String[] args) {

    // Creating a HashMap
    HashMap<String, String> languages = new HashMap<>();
    languages.put("Java", "Enterprise");
    languages.put("Python", "ML/AI");
    languages.put("JavaScript", "Frontend");
    System.out.println("HashMap: " + languages);

    // iterating through key/value mappings
    System.out.print("Entries: ");
    for(Entry<String, String> entry: languages.entrySet()) {
      System.out.print(entry);
      System.out.print(", ");
    }

    // iterating through keys
    System.out.print("\nKeys: ");
    for(String key: languages.keySet()) {
      System.out.print(key);
      System.out.print(", ");
    }

    // iterating through values
    System.out.print("\nValues: ");
    for(String value: languages.values()) {
      System.out.print(value);
      System.out.print(", ");
    }
  }
}

Output

HashMap: {Java=Enterprise, JavaScript=Frontend, Python=ML/AI}
Entries: Java=Enterprise, JavaScript=Frontend, Python=ML/AI, 
Keys: Java, JavaScript, Python,
Values: Enterprise, Frontend, ML/AI,

In the above example, we have created a hashmap named languages. Here, we have used the forEach loop to iterate through the elements of the hashmap.

Notice that we are independently iterating through the keys, values, and key/value mappings.

Note: We have used the Map.Entry class. It is the nested class that returns a view of the map.


Example 2: Iterate through HashMap using iterator()

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

class Main {
  public static void main(String[] args) {
    // create a HashMap
    HashMap<String, String> languages = new HashMap<>();
    languages.put("Java", "Enterprise");
    languages.put("Python", "ML/AI");
    languages.put("JavaScript", "Frontend");
    System.out.println("HashMap: " + languages);

    // create an object of Iterator
    Iterator<Entry<String, String>> iterate1 = languages.entrySet().iterator();

    // iterate through key/value mappings
    System.out.print("Entries: ");
    while(iterate1.hasNext()) {
      System.out.print(iterate1.next());
      System.out.print(", ");
    }

    // iterate through keys
    Iterator<String> iterate2 = languages.keySet().iterator();
    System.out.print("\nKeys: ");
    while(iterate2.hasNext()) {
      System.out.print(iterate2.next());
      System.out.print(", ");
    }

    // iterate through values
    Iterator<String> iterate3 = languages.values().iterator();
    System.out.print("\nValues: ");
    while(iterate3.hasNext()) {
      System.out.print(iterate3.next());
      System.out.print(", ");
    }
  }
}

Output

HashMap: {Java=Enterprise, JavaScript=Frontend, Python=ML/AI}
Entries: Java=Enterprise, JavaScript=Frontend, Python=ML/AI, 
Keys: Java, JavaScript, Python,
Values: Enterprise, Frontend, ML/AI,

In the above example, we are iterating through keys, values, and key/value mappings of the hash map. We have used the iterator() method to iterate over the hashmap. Here,

  • hasNext() - returns true if there is next element in the hashmap
  • next() - returns the next element of the hashmap

Note: We can also use the HashMap forEach() method to iterate over the hashmap.

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community