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.

One comment

  1. vcode: 03AOLTBLSH6xlV_Xzq9OQE-lt09dazZ2waOm836U_t4ofVZuAs6FDAMlx9o0ANb9H8YmIO77VB2OamfcBO9VAQSz_LtkrLwgqoSWuH4LKl_zvpGBei_P_skQYPejaEpfd61XU5EtZPaWZjMNI9HAO35qtybcLUb806-Of_0_VzUxwBEY-aOIyPd0WdvF-6W9syImIZrxAh8hn6Q_f5CVUTNaDmbAEGHkulOMFXTpX_UnoCzN8KF_bZBOT0GMySc4sWj8rBrqs4geyOftGYmYvU9d6RiQpzL_mWpOqkOGYgbNZYSeamAkOL6KTKV0xoR5zDfoN_4eKc0nvH_PTeRrXDAgzClL5S0bxwCpCZBTS5SraP73oM7vP6CCVgIdD9bTFSIu_WMn049GL6cfcEppy-PpTPEF-PQDRE-sU6pvhbWqx_nvpBWvVKA4VBKl69uSo9c-n2d5XcLGaWUZfyQv7v2eqky-lohC68cBIy891cS7lUdLVNBcCj1EyZ0AOgHyOXOpzvllqcIQsVccPMBM7JbqFdPZEgO8SAP-3SE3gqdBMqiQdyZ3S343_HHv9F-qotLePsn7hIMGMYhD_lMkIG1Rz-zJWHY00vqA

Leave a Reply

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