A simple way for Java to iterate over a HashMap

  • 2020-04-01 03:39:49
  • OfStack

This article illustrates a simple way for Java to traverse a HashMap. Share with you for your reference. The specific implementation method is as follows:


import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Set; 
public class HashSetTest { 
  public static void main(String[] args) { 
    HashMap map = new HashMap(); 
    map.put("a", "aa"); 
    map.put("b", "bb"); 
    map.put("c", "cc"); 
    map.put("d", "dd"); 
    map.put("e", "ee"); 
    Set set = map.keySet(); 
    for(Iterator itr=set.iterator();itr.hasNext();){ 
      String value =(String) itr.next(); 
      String key = (String)map.get(value); 
      System.out.println(value+"="+key);  
     } 
  } 
}

I hope this article has been helpful to your Java programming.


Related articles: