WHAT IS THE BEST WAY TO ITERATE ON HASHMAP IN JAVA

Felix Pickles

Total Post:24

Points:168
Posted by  Felix Pickles
 808  View(s)
Ratings:
Rate this:
What's the best way to iterate over the items in a HashMap?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: What is the best way to iterate on hashmap in java

    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
        }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!