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

Total Post:24

Posted on    July-27-2015 5:41 AM

 Java Java  Collection 

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

Mayank Tripathi
Mayank Tripathi

Total Post:397

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