How to iterate HashMap in Java ?

 

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();
       }
}

HashMap:

Java HashMap class implements the map interface by using a HashTable. It inherits AbstractMap class and implements Map interface.

The main characterstics of Java HashMap are:

  • A HashMap contains values based on the key.
  • It contains only unique entities.
  • It can have one null key and multiple null values.
  • It has no order.

Join Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.