{"id":670,"date":"2016-02-29T10:54:08","date_gmt":"2016-02-29T10:54:08","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=670"},"modified":"2016-02-29T10:54:08","modified_gmt":"2016-02-29T10:54:08","slug":"hashmap-methods-examples","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/hashmap-methods-examples.html","title":{"rendered":"HashMap Methods Tutorials in Java With Examples"},"content":{"rendered":"<p>HashMap is a one-to-one relationship between one object and other object. Let us take a real life example of hash map for better understanding of this concept:<\/p>\n<p>Let&#8217;s assume you want to open\u00a0your email account. But to do that\u00a0you&#8217;re taken to a login screen where you type in your username and password. When the combination is correct only then your email account open.<\/p>\n<p>A hashmap works in a similar way. You have to give it an appropriate input so you can get the proper output. Each input is mapped to exactly one output. In our previous example, one combination of username and password produces exactly one email account.<\/p>\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<hr \/>\n<h4><strong>HashMap Methods in JAVA:<\/strong><\/h4>\n<p>Let us discuss all the 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><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/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><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/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><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Size of hashmap is 3\r\n\r\nSize of hashmap after clearing is 0<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. Object clone():<\/strong><\/span><\/p>\n<p>This method returns the exact copy 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n \/\/Creating a new hashmap\r\n\r\n  HashMap hashmapobj2 = new HashMap();\r\n\r\n \/\/Using clone method\r\n\r\n  hashmapobj2=(HashMap)hashmapobj1.clone();\r\n\r\n  System.out.println(\"Cloned 2nd Map: \" + hashmapobj2);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Cloned 2nd Map: {1001=I, 1002=Love, 1003=Java}<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. boolean containsKey(Object key):<\/strong><\/span><\/p>\n<p>This method checks the presence of specified\u00a0 key in the HashMap, it will return true if the key is present and false if the mentioned key is not present, as shown in the following program:<\/p>\n<pre>package com.test;\r\n\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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  \/\/ check existence of key 1002\r\n\r\n  System.out.println(\"Check if key 1002 exists: \" + hashmapobj1.containsKey(1002));\r\n\r\n  \/\/ check existence of key 1000\r\n\r\n  System.out.println(\"Check if key 1000 exists: \" + hashmapobj1.containsKey(1000));\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Check if key 1002 exists: true\r\n\r\nCheck if key 1000 exists: false<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. boolean containsValue(Object value):<\/strong><\/span><\/p>\n<p>This method checks the presence of specified value in the HashMap, it will return true if the value is present and false if the mentioned value is not present, 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n \/\/ check existence of value 'I'\r\n\r\n  System.out.println(\"Check if value 'I' exists: \" +\u00a0 hashmapobj1.containsValue(\"I\"));\r\n\r\n\/\/ check existence of value 'India'\r\n\r\n  System.out.println(\"Check if value 'India' exists: \" +\u00a0 hashmapobj1.containsValue(\"India\"));\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Check if value 'I' exists: true\r\n\r\nCheck if value 'India' exists: false<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. Set entrySet():<\/strong><\/span><\/p>\n<p>This method returns a Set view of the mappings contained in the HashMap, as shown in the following program:<\/p>\n<pre>package com.test;\r\n\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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  \/\/ create set of hashmap\r\n\r\n  Set set=hashmapobj1.entrySet();\r\n\r\n  System.out.println(\"Set values: \" + set);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Set values: [1001=I, 1002=Love, 1003=Java]<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. Object get(Object key):<\/strong><\/span><\/p>\n<p>This method returns the value corresponding to the specified key, 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  \/\/ get value of key 1003\r\n\r\n  String str=(String)hashmapobj1.get(1003);\r\n\r\n  System.out.println(\"Value for key 1003 is: \" + str);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Value for key 1003 is: Java<\/pre>\n<p><span style=\"color: #008000;\"><strong>9.boolean isEmpty():<\/strong><\/span><\/p>\n<p>This method as the name suggests checks whether the HashMap is empty or not, It will return true if it is empty, or false if it is not empty, 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  \/\/ check if the map is empty or not\r\n\r\n  System.out.println(\" Hashmap is empty??\" + hashmapobj1.isEmpty());\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Hashmap is empty??false<\/pre>\n<p><span style=\"color: #008000;\"><strong>10. Set keySet():<\/strong><\/span><\/p>\n<p>This method returns a Set view of the keys contained 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  \/\/ seting the keyset\r\n\r\n  Set keyset=hashmapobj1.keySet();\r\n\r\n  \/\/ check key set values\r\n\r\n  System.out.println(\"Key values are: \" + keyset);\r\n\r\n }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Key values are: [1001, 1002, 1003]<\/pre>\n<p><span style=\"color: #008000;\"><strong>11. putAll(Map m):<\/strong><\/span><\/p>\n<p>This method copies all the key value pair form one hashmap to another , 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  System.out.println(\"Values in newmap1: \"+ hashmapobj1);\r\n\r\n  HashMap hashmapobj2 = new HashMap();\r\n\r\n  \/\/ put all values in newmap2\r\n\r\n  hashmapobj2.putAll(hashmapobj1);\r\n\r\n  System.out.println(\"Values in hashmapobj2: \"+ hashmapobj2);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in newmap1: {1001=I, 1002=Love, 1003=Java}\r\n\r\nValues in hashmapobj2: {1001=I, 1002=Love, 1003=Java}<\/pre>\n<p><span style=\"color: #008000;\"><strong>12. Object remove(Object key):<\/strong><\/span><\/p>\n<p>This method as the name specifies removes the key value pair, corresponding to the mentioned key in the argument list, 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n  System.out.println(\"hashmap before\u00a0 using remove method: \"+ hashmapobj1);\r\n\r\n  \/\/ remove value for key 1002\r\n\r\n  hashmapobj1.remove(1002);\r\n\r\n  System.out.println(\"hashmap after\u00a0 using remove method: \"+ hashmapobj1);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>hashmap before\u00a0 using remove method: {1001=I, 1002=Love, 1003=Java}\r\n\r\nhashmap after\u00a0 using remove method: {1001=I, 1003=Java}<\/pre>\n<p><span style=\"color: #008000;\"><strong>13. Collection values():<\/strong><\/span><\/p>\n<p>This method returns the collection of all the values 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 hashmapobj1 = new HashMap();\r\n\r\n  hashmapobj1.put(1001, \"I\");\r\n\r\n  hashmapobj1.put(1002, \"Love\");\r\n\r\n  hashmapobj1.put(1003, \"Java\");\r\n\r\n  System.out.println(\"collection view of this hashmap is \"+ hashmapobj1.values());\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>collection view of this hashmap is [I, Love, Java]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HashMap is a one-to-one relationship between one object and other object. Let us take a real life example of hash map for better understanding of this concept: Let&#8217;s assume you want to open\u00a0your email account. But to do that\u00a0you&#8217;re taken to a login screen where you type in your username and password. When the combination &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/hashmap-methods-examples.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HashMap Methods Tutorials in Java With Examples<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,77],"tags":[],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-collections"],"psp_head":"<title>HashMap Methods Tutorials in Java With Examples \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"HashMap is a one-to-one relationship between one object and other object, generally denoted as HashMap  or HashMap . It implements Map Interface and Extends AbstractMap class,\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/hashmap-methods-examples.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"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=670"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}