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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s