{"id":673,"date":"2016-02-28T06:35:15","date_gmt":"2016-02-28T06:35:15","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=673"},"modified":"2018-06-05T06:30:36","modified_gmt":"2018-06-05T06:30:36","slug":"hashmap","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/hashmap","title":{"rendered":"HashMap Tutorial In Java with Example"},"content":{"rendered":"<p>HashMap is a type of Collection, that stores our data in a pair such that each element has a key associated with it. The pair of key and value is often known as Entry and these entries can have only unique keys.<\/p>\n<p>HashMap is a class that implements Map Interface and Extends AbstractMap class which provides the basic structural implementation of Map Interface which minimizes the efforts that are required to implement the Map interface directly in our HashMap Class.<\/p>\n<p><strong>It is generally denoted as HashMap &lt;key, value&gt; or HashMap &lt;K,V&gt;<\/strong>.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span>\u00a0HashMap allows null key and null value in it, but with a restriction that there can be only one null key and multiple null values.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of HashMap Class<\/strong><\/h4>\n<p>HashMap Class extends AbstractMap Class and that Implements Map Interface as shown in\u00a0 figure 1.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-732\" src=\"\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-hashmap.png\" alt=\"hierarchy of hashmap\" width=\"221\" height=\"327\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-hashmap.png 221w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-hashmap-203x300.png 203w\" sizes=\"auto, (max-width: 221px) 100vw, 221px\" \/><\/center><\/p>\n<hr \/>\n<h4>\u00a0<strong>Examples of HashMap class<\/strong><\/h4>\n<p>Let us discuss Hash map with the help of following\u00a0programs.<\/p>\n<p>In this\u00a0program we have created object of HashMap Class, added key value pairs using put method, and displayed it using getKey and getValue methods , as shown\u00a0below<\/p>\n<pre class=\"\">import java.util.*;\r\n\r\npublic class HashMapDemoClass{\r\n\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 1: Defing object of HashMap Class\r\n\r\nHashMap&lt;Integer,String&gt; HashMap=new HashMap&lt;Integer,String&gt;();\r\n\r\n\/\/Step 2: Adding Key Value pair\r\n\r\nHashMap.put(1001,\"India\");\r\n\r\nHashMap.put(1002,\"Canada\");\r\n\r\nHashMap.put(1003,\"Australia\");\r\n\r\n\/\/Step 3: Displaying key value pairs using for loop\r\n\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() )\r\n\r\n{\r\n\r\n\/\/Step 4: Using getKey and getValue methods to retrieve key and corresponding value\r\n\r\nSystem.<em>out<\/em>.println(map.getKey()+\" \"+map.getValue());\r\n\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Australia\r\n<\/pre>\n<p><strong>Description:<\/strong><\/p>\n<ul>\n<li>In Step 1, we have created an object of HashMap collection, As we have already discussed HashMap has key value pair, so obviously we have to define the type of key and value, We have defined Key of Integer type, and Value of String type.<\/li>\n<li>In Step 2, we have used put method to add key value pair in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have used for each loop of following syntax to retrieve the values from the HashMap object<\/li>\n<\/ul>\n<p><strong>for(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet())<\/strong><\/p>\n<p>In this for loop we have used method entrySet(), which returns a Set view of the mappings contained in the HashMap object and the Map.Entry is an Interface that enables you to work with a map entry easily by which key-value pairs are retrieved one by one, and are displayed in next step.<\/p>\n<ul>\n<li>In Step 4 we have used two methods , as shown<\/li>\n<\/ul>\n<p><strong>System.<em>out<\/em>.println(map.getKey()+&#8221; &#8220;+map.getValue());<\/strong><\/p>\n<p>First is\u00a0 getKey() , which as name suggest retrieves the key and secondly getValue() method that retrieves the value corresponding to the given key.<\/p>\n<hr \/>\n<h4><strong>Adding Entry with Duplicate Key in HashMap<\/strong><\/h4>\n<p>HashMap does not allow Entry with duplicate key, it overlaps old value with new one. Below program helps you understand this.<\/p>\n<p>In this program we have created object of HashMap Class and added key value pairs, one with duplicate key, as shown in the following program<\/p>\n<pre class=\"\">import java.util.*;\r\npublic class HashMapDemoClass{\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 1: Defining object of HashMap Class\r\nHashMap&lt;Integer,String&gt; HashMap=new HashMap&lt;Integer,String&gt;();\r\n\r\n\/\/Step 2: Adding Key Value pair\r\n\r\nHashMap.put(1001,\"India\");\r\n\r\nHashMap.put(1002,\"Canada\");\r\n\r\nHashMap.put(1003,\"Australia\");\r\n\r\n\/\/Step 3: Displaying key value pairs using for loop\r\n\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() )\r\n\r\n{\r\n\r\n\/\/Step 4: Using getKey and getValue methods to retrieve key and corresponding value\r\n\r\nSystem.out.println(map.getKey()+\" \"+map.getValue());\r\n\r\n}\r\n\r\n\/\/Step 5: Adding element with duplicate key\r\n\r\nHashMap.put(1003,\"Nepal\");\r\n\r\n\u00a0\r\n\/\/Step 6: Displaying Elemnets\r\n\r\nSystem.out.println(\"Entries after adding element with Duplicate Key\");\r\n\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() )\r\n\r\n{\r\n\r\nSystem.out.println(map.getKey()+\" \"+map.getValue());\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Australia\r\n\r\nEntries after adding element with Duplicate Key\r\n\r\n1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Nepal\r\n<\/pre>\n<p><strong>Conclusion:<\/strong>\u00a0From output it is clear that HashMap does not allow Entry with duplicate key, it overlaps old value with new one, as shown in the output.<\/p>\n<p><strong>Description of Program:<\/strong><\/p>\n<ul>\n<li>In Step 1, we have created an object of HashMap collection<\/li>\n<li>In Step 2, we have used put method to add key value pair in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have used for each loop to retrieve the values from the HashMap object<\/li>\n<li>In Step 4 we have displayed entries , with getkey() and getvalue() \u00a0methods that are discussed above<\/li>\n<li>In Step 5, we have added an entry with duplicate key<\/li>\n<li>In Step 6, we have again displayed all the entries<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Adding Entry with Duplicate Value in HashMap<\/strong><\/h4>\n<p>HashMap allows Entry with duplicate value\u00a0but having unique key.<\/p>\n<p>In this program we have created object of HashMap Class and added key value pairs, one with duplicate value, as shown in the following program<\/p>\n<pre>import java.util.*;\r\npublic class HashMapDemoClass{\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 1: Defing object of HashMap Class\r\n\r\nHashMap&lt;Integer,String&gt; HashMap=new HashMap&lt;Integer,String&gt;();\r\n\u00a0\r\n\r\n\/\/Step 2: Adding Key Value pair\r\n\r\nHashMap.put(1001,\"India\");\r\n\r\nHashMap.put(1002,\"Canada\");\r\n\r\nHashMap.put(1003,\"Australia\");\r\n\r\n\/\/Step 3: Displaying key value pairs using for loop\r\n\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() )\r\n\r\n{\r\n\r\n\/\/Step 4: Using getKey and getValue methods to retrieve key and corresponding value\r\n\r\nSystem.out.println(map.getKey()+\" \"+map.getValue());\r\n\r\n}\r\n\r\n\/\/Step 5: Adding element with duplicate value\r\n\r\nHashMap.put(1004,\"India\");\r\n\r\n\/\/Step 6: Displaying Elemnets\r\n\r\nSystem.out.println(\"Entries after adding element with Duplicate Value\");\r\n\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() )\r\n\r\n{\r\n\r\nSystem.out.println(map.getKey()+\" \"+map.getValue());\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Australia\r\n\r\nEntries after adding element with Duplicate Value\r\n\r\n1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Australia\r\n\r\n1004 India\r\n<\/pre>\n<p><strong>Conclusion:<\/strong>\u00a0From output it is clear that HashMap allows Entry with duplicate value.<\/p>\n<p><strong>Description:<\/strong><\/p>\n<ul>\n<li>In Step 1, we have created an object of HashMap collection<\/li>\n<li>In Step 2, we have used put method to add key value pair in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have used for each loop to retrieve the values from the HashMap object<\/li>\n<li>In Step 4 we have displayed entries , with getkey() and getvalue() methods that are discussed above<\/li>\n<li>In Step 5, we have added an entry with duplicate value<\/li>\n<li>In Step 6, we have again displayed all the entries<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>HashMap Methods in JAVA:<\/strong><\/h4>\n<p>HashMap is a class that implements Map Interface and Extends AbstractMap class.\u00a0\u00a0It is generally denoted as HashMap &lt;key, value&gt; or HashMap &lt;K,V&gt;.<\/p>\n<p>Let us discuss HashMap methods one by one with Examples in Java.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. Object put(Object key, Object value):<\/strong><\/span><\/p>\n<p>This method adds the key value pair to the HashMap object, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashMapDemoClass \r\n{\r\n\r\n  public static void main(String args[]) {\r\n\r\n\/\/ create a hashmap\r\n\r\n  HashMap hashmapobj = new HashMap();\r\n\r\n\/\/ using Object put(Object key, Object value) method\r\n\r\n  hashmapobj.put(1001, \"I\");\r\n\r\n  hashmapobj.put(1002, \"Love\");\r\n\r\n  hashmapobj.put(1003, \"Java\");\r\n\r\n\/\/printing the hashmap\r\n\r\n  System.out.println(\" All the key value pairs \" + hashmapobj);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>All the key value pairs {1001=I, 1002=Love, 1003=Java}<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. int size():<\/strong><\/span><\/p>\n<p>This method returns the size of the HashMap as shown in the following program:<\/p>\n<pre>package com.test;\r\nimport java.util.*;\r\n\r\npublic class HashMapDemoClass \r\n{\r\n\r\n  public static void main(String args[]) {\r\n\r\n \/\/ create a hashmap\r\n\r\n  HashMap hashmapobj = new HashMap();\r\n\r\n  hashmapobj.put(1001, \"I\");\r\n\r\n  hashmapobj.put(1002, \"Love\");\r\n\r\n  hashmapobj.put(1003, \"Java\");\r\n\r\n  \/\/using size() method\r\n\r\n  System.out.println(\" Size of hashmap is \" + hashmapobj.size());\r\n\r\n }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Size of hashmap is 3<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. void clear():<\/strong><\/span><\/p>\n<p>This method clears all the key value pairs in the HashMap, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashMapDemoClass \r\n{\r\n\r\n   public static void main(String args[]) {\r\n\r\n  \/\/ create a hashmap\r\n\r\n   HashMap hashmapobj = new HashMap();\r\n\r\n   hashmapobj.put(1001, \"I\");\r\n\r\n   hashmapobj.put(1002, \"Love\");\r\n\r\n   hashmapobj.put(1003, \"Java\");\r\n\r\n  \/\/using size() method\r\n\r\n   System.out.println(\" Size of hashmap is \" + hashmapobj.size());\r\n\r\n   \/\/using clear() method\r\n\r\n   hashmapobj.clear();\r\n\r\n  \/\/using size() method\u00a0 after clearing the hashmap\r\n\r\n   System.out.println(\" Size of hashmap after clearing is \" + hashmapobj.size());\r\n\r\n }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Size of hashmap is 3\r\n\r\nSize of hashmap after clearing is 0<\/pre>\n<p>Read all <a href=\"\/java\/hashmap\">HashMap Methods in JAVA<\/a><\/p>\n<hr \/>\n<h4><strong>Important Points About HashMap<\/strong><\/h4>\n<ul>\n<li>For using HashMap you must need to import util.HashMap or its super class\u00a0.<\/li>\n<li>HashMap only stores values with unique keys, that is duplicate keys are not allowed.<\/li>\n<li>HashMap do not maintains order, which means it does not return the key value pair in the order in which they have been added.<\/li>\n<li>It does not do any kind of sorting to the stored keys and values.<\/li>\n<li>HashMap allows null key and null value in it,but with a restriction that there can be only one null key and multiple null values.<\/li>\n<li>HashMap is very much similar to HashTable, but with few differences, firstly HashMap is not thread safe, where as HashTable is thread safe. Secondly, HashMap is unsynchronized, where as HashTable is, So performance of HashMap is better than HashTable.<\/li>\n<li>To add thread safety or we can say to make HashMap synchronized we have class ConcurrentHashMap, which makes it threadsafe and synchronized.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>HashMap Quick Revision Points<\/strong><\/h4>\n<ul>\n<li>Data is stores in key value pair.<\/li>\n<li>Duplicate keys are not allowed.<\/li>\n<li>No insertion order is maintained.<\/li>\n<li>It can have only one null key and multiple null values.<\/li>\n<li>It is not thread safe.<\/li>\n<li>It is unsynchronized.<\/li>\n<li>It performs better as compared to HashTable.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>HashMap is a type of Collection, that stores our data in a pair such that each element has a key associated with it. The pair of key and value is often known as Entry and these entries can have only unique keys. HashMap is a class that implements Map Interface and Extends AbstractMap class which &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/hashmap\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HashMap Tutorial In Java with Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-673","page","type-page","status-publish","hentry"],"psp_head":"<title>HashMap Class Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on HashMap Class which is key value pairs with example, program and code in JAVA. Also find details of what happens after adding Entries with duplicate Key or value in HashMap.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/hashmap\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=673"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/673\/revisions"}],"predecessor-version":[{"id":1474,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/673\/revisions\/1474"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}