Home > DeveloperSection > Forums > What is the best way to iterate on hashmap in java
Felix Pickles
Felix Pickles

Total Post:24

Points:168
Posted on    July-27-2015 5:41 AM

 Java Java  Collection 
Ratings:


 1 Reply(s)
 490  View(s)
Rate this:
What's the best way to iterate over the items in a HashMap?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-27-2015 8:36 AM

Iterate through the entrySet like so:
public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick