4 Different ways to iterate over HashMap in Java

This articles discusses different approaches of iterating over a Map or HashMap in Java.

iterate over hashmap in java
 

1. Iterate HashMap using For-Each loop and Map.Entry

This is a legacy technique for traversing using for-each loop over each Map.Entry.

private static void iterateUsingForEach(Map<String, String> countryCapitalMap){
  for (Map.Entry<String, String> entry : countryCapitalMap.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " , Value : " + entry.getValue());
  }
}

 

2. Iterate HashMap using Iterator and Map.Entry

We can also use an Iterator (java.util.Iterator) to loop over the hashmap entries.

private static void iterateUsingIterator(Map<String, String> countryCapitalMap){
  Iterator<Map.Entry<String, String>> entries = countryCapitalMap.entrySet().iterator();
  while (entries.hasNext()) {
      Map.Entry<String, String> entry = entries.next();
      System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
  }
}

 

3. Iterate HashMap using forEach and Lambda expression

In Java 8 , we can combine the new forEach syntax with lambda expression for iterating over a hashmap.

private static void iterateUsingLambda(Map<String, String> countryCapitalMap){
  countryCapitalMap.forEach((k,v) -> System.out.println("Key : " + k + " , Value : " + v));
}

 

4. Iterate HashMap using Java 8 Stream api

private static void iterateUsingStreamApi(Map<String, String> countryCapitalMap){
  countryCapitalMap.entrySet().stream().forEach(e -> System.out.println("Key : " + e.getKey() + " , Value : " + e.getValue()));
}

Here is the complete example

package com.topjavatutorial;

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

public class MapIterateExample {

  public static void main(String[] args) {
    Map<String, String> countryCapitalMap = new HashMap<String, String>();
    countryCapitalMap.put("guyana", "georgetown");
    countryCapitalMap.put("nepal", "kathmandu");
    countryCapitalMap.put("australia", "canberra");
    countryCapitalMap.put("india", "new delhi");
    countryCapitalMap.put("japan", "tokyo");

    System.out.println("Iterate Map using forEach loop");
    iterateUsingForEach(countryCapitalMap);
    
    System.out.println("\nIterate Map using Iterator");
    iterateUsingIterator(countryCapitalMap);
    
    System.out.println("\nIterate Map using Lambda expression with forEach");
    iterateUsingLambda(countryCapitalMap);
    
    System.out.println("\nIterate Map using Java 8 Stream api");
    iterateUsingStreamApi(countryCapitalMap);
  }
  private static void iterateUsingForEach(Map<String, String> countryCapitalMap){
    for (Map.Entry<String, String> entry : countryCapitalMap.entrySet()) {
      System.out.println("Key : " + entry.getKey() + " , Value : " + entry.getValue());
    }
  }
  
  private static void iterateUsingIterator(Map<String, String> countryCapitalMap){
    Iterator<Map.Entry<String, String>> entries = countryCapitalMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
  }
  
  private static void iterateUsingLambda(Map<String, String> countryCapitalMap){
    countryCapitalMap.forEach((k,v) -> System.out.println("Key : " + k + " , Value : " + v));
  }
  
  private static void iterateUsingStreamApi(Map<String, String> countryCapitalMap){
    countryCapitalMap.entrySet().stream().forEach(e -> System.out.println("Key : " + e.getKey() + " , Value : " + e.getValue()));
  }
}

Output

Iterate Map using forEach loop
Key : guyana , Value : georgetown
Key : japan , Value : tokyo
Key : nepal , Value : kathmandu
Key : australia , Value : canberra
Key : india , Value : new delhi

Iterate Map using Iterator
Key = guyana, Value = georgetown
Key = japan, Value = tokyo
Key = nepal, Value = kathmandu
Key = australia, Value = canberra
Key = india, Value = new delhi

Iterate Map using Lambda expression with forEach
Key : guyana , Value : georgetown
Key : japan , Value : tokyo
Key : nepal , Value : kathmandu
Key : australia , Value : canberra
Key : india , Value : new delhi

Iterate Map using Java 8 Stream api
Key : guyana , Value : georgetown
Key : japan , Value : tokyo
Key : nepal , Value : kathmandu
Key : australia , Value : canberra
Key : india , Value : new delhi

 

© 2016 – 2019, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags