{"id":1180,"date":"2016-03-19T09:18:32","date_gmt":"2016-03-19T09:18:32","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1180"},"modified":"2016-03-19T09:18:32","modified_gmt":"2016-03-19T09:18:32","slug":"map","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/map","title":{"rendered":"Map Interface in Java With Example"},"content":{"rendered":"<p>The\u00a0Map\u00a0Interface is the mapping between a key and a value i.e. it contains values based on the key. Each key value pair is often known as an entry, As Map contains only unique keys, we can also say Map interface maps unique keys to its corresponding values. With the key we can retrieve a value when we require later.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span>\u00a0The Map interface do not Extends the Collection\u00a0interface<\/p>\n<hr \/>\n<h4><strong>Map Implementations<\/strong><\/h4>\n<p><span style=\"color: #0000ff;\"><strong>1. HashMap<\/strong><\/span><\/p>\n<p>HashMap is a class that implements Map Interface and Extends AbstractMap class. HashMap stores our data in a pair such that each element has a key associated with it.<\/p>\n<p>It is generally denoted as HashMap &lt;key, value&gt; or HashMap &lt;K,V&gt;.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example of HashMap Implementation :<\/strong><\/span><\/p>\n<p>Let us discuss HashMap Implementation with the help of program, following program has been divided into 4 Steps that we will discuss one by one.<\/p>\n<pre>import java.util.*;\r\npublic class HashMapDemoClass{\r\n\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 1: Object of HashMap is Created\r\nHashMap&lt;Integer,String&gt; HashMap=new HashMap&lt;Integer,String&gt;();\u00a0 \r\n\r\n\/\/Step 2: Data is entered using Put() method\r\nHashMap.put(1001,\"India\");\u00a0\u00a0\u00a0\u00a0\r\nHashMap.put(1002,\"Canada\");\r\nHashMap.put(1003,\"Australia\");\r\n\r\n\u00a0\/\/Step 3: Used for Each loop to iterate\r\nfor(Map.Entry map\u00a0 :\u00a0 HashMap.entrySet() ) \r\n\r\n{\r\n\/\/Step 4: used getKey() and getValue() methods\r\nSystem.<em>out<\/em>.println(map.getKey()+\" \"+map.getValue());\u00a0\u00a0 \r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>1001 India\r\n\r\n1002 Canada\r\n\r\n1003 Australia\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description of Example:<\/strong><\/span><\/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 to retrieve the values from the HashMap. We have used method entrySet() which returns the Set containing all the keys and values<\/li>\n<li>In Step 4 we have used two methods , First is getKey() , which as name suggest retrieves the key and secondly getValue() method that retrieves the values corresponding to the keys.<\/li>\n<\/ul>\n<p>For more read <a href=\"\/java\/hashmap\">HashMap tutorial<\/a><\/p>\n<p><span style=\"color: #0000ff;\"><strong>2. LinkedHashMap<\/strong><\/span><\/p>\n<p>LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class\u00a0i.e.\u00a0it stores our data in a pair such that each element has a key associated with it. As, HashMap does not maintains the insertion order, that is when we retrieve values from it we do not get that values in the same order we have entered in it.\u00a0So, The functionality of maintaining the insertion is added into LinkedHashMap. To attain this\u00a0 functionality all the entries(key and value) are linked to each other using doubly-linked list. This doubly-linked list maintains the iteration ordering, which is in general the order in which keys were added in\u00a0 the map.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example of LinkedHashMap Implementation :<\/strong><\/span><\/p>\n<p>Let us discuss LinkedHashMap Implementation with the help of program, following program has been divided into 4 Steps that we will discuss one by one.<strong>\u00a0<\/strong><\/p>\n<pre>import java.util.LinkedHashMap;\r\nimport java.util.Map;\r\n\r\npublic class LinkedHashMapDemo {\r\npublic static void main(String[] args) {\r\n\r\n\/\/Step 1: linkedHashMap object is created\r\nMap&lt;String, Integer&gt; linkedHashMapobject = new LinkedHashMap&lt;String, Integer&gt;();\r\n\/\/Step 2: Elements are entered using put() method\r\nlinkedHashMapobject.put(\"Samsung Grand quattro price \", new Integer(10000));\r\nlinkedHashMapobject.put(\"Micromax canvas price\", new Integer(9000));\r\nlinkedHashMapobject.put(\"Sony T2 Ultra price\", new Integer(20000));\r\nlinkedHashMapobject.put(\"Nokia Lumia price\", new Integer(15000));\r\nlinkedHashMapobject.put(\"Microsoft Lumia price \", new Integer(16000));\r\n\r\n\/\/Step 3: printing contents of linkedhashMap\r\nSystem.out.println(\"Contents of LinkedHashMap : \" + linkedHashMapobject);\r\n\u00a0\r\nSystem.out.println(\"\\nValues of linkedHashMapobject after iterating over it : \\n\");\r\n\r\n\/\/Step 4: using for each loop to iterate over linkedHashMap\r\nfor (String key : linkedHashMapobject.keySet()) {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\nSystem.out.println(key + \u201c:\\t\u201d + linkedHashMapobject.get(key));\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Contents of LinkedHashMap : {Samsung Grand uattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}\r\n\u00a0\r\nValues of linkedHashMapobject after iterating over it :\r\n\r\nSamsung Grand uattro price :\u00a0\u00a0\u00a0 10000\r\n\r\nMicromax canvas price:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9000\r\n\r\nSony T2 Ultra price:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20000\r\n\r\nNokia Lumia price:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 15000\r\n\r\nMicrosoft Lumia price :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 16000\r\n\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description of Example:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we are creating an object linkedHashMap object that is storing key of String type and value of Integer type<\/li>\n<li>In Step 2, we are using put method to add the key value pair in the object we have created in step 1.<\/li>\n<li>In Step 3 we are printing\u00a0 the contents of the LinkedHashMap<\/li>\n<li>In Step 4 we are using for each loop to iterate over the linkedhashmap object , we are using keySet and get(key) method to diplay all the values of map, we will discuss each method in detail in next topic.<\/li>\n<\/ul>\n<p>For more details read <a href=\"\/java\/linkedhashmap\">LinkedHashMap tutorial<\/a><\/p>\n<p><span style=\"color: #0000ff;\"><strong>3.\u00a0TreeMap<\/strong><\/span><\/p>\n<p>TreeMap also stores values based on the corresponding key. It stores only unique keys, that is duplicate keys are not allowed. Also it cannot store key as <strong>null<\/strong> but it can store<strong> null <\/strong>values. It is mostly similar to HashMap except\u00a0it maintains natural order of keys. If key is of integer type it arranges them in increasing order, or if they are of string type, it arranges them in dictionary order.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example of TreeMap Implementation :<\/strong><\/span><\/p>\n<p>Let us discuss TreeMap Implementation with the help of program, following program has been divided into 4 Steps that we will discuss one by one.<\/p>\n<pre>\u00a0\r\nimport java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\n\u00a0\/\/Step 1: object of TreeMap is created\r\nTreeMap&lt;Integer,String&gt; treemap=new TreeMap&lt;Integer,String&gt;();\u00a0 \r\n\r\n\/\/Step 2: Elements are entered using put() method\r\ntreemap.put(102,\"Core\");\u00a0 \r\ntreemap.put(100,\"I\");\r\ntreemap.put(103,\"Java\");\r\ntreemap.put(101,\"Love\");\r\n\r\n\/\/Step 3: used for each loop to iterate over Map\r\nfor(Map.Entry map\u00a0 : treemap.entrySet()){\u00a0\u00a0\r\n\/\/Step 4: used getKey() and getValue() method\r\nSystem.<em>out<\/em>.println(map.getKey()+\" \"+map.getValue());\u00a0 \r\n\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>100 I\r\n\r\n101 Love\r\n\r\n102 Core\r\n\r\n103 Java\r\n<\/pre>\n<p>In output,We have obtained output in ascending order based on the Key, which distinguishes it from hash map<\/p>\n<p><span style=\"color: #008000;\"><strong>Description of Example:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we have created an object of Tree Map collection, 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 to retrieve the values from the Tree Map. We have used method entrySet() which returns the Set containing all the keys and values.<\/li>\n<li>In Step 4 we have used two methods , First is getKey () , which as name suggest retrieves the key and secondly getValue () method that retrieves the values corresponding to the keys.<\/li>\n<\/ul>\n<p>For more details read <a href=\"\/java\/treemap\">TreeMap tutorial<\/a><\/p>\n<hr \/>\n<h4><strong>Map Methods<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. void clear( )<\/strong><\/span><\/p>\n<p>This method Removes all entries from the \u00a0map object.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. boolean containsKey(Object k)<\/strong><\/span><\/p>\n<p>This method returns true if the given map contains k as a key else it returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. boolean containsValue(Object v)<\/strong><\/span><\/p>\n<p>This method returns true if the given map contains v as a value else it returns false<\/p>\n<p><span style=\"color: #008000;\"><strong>4. Set entrySet( )<\/strong><\/span><\/p>\n<p>This method returns a Set that contains the entries in the map. This method provides a set view of the invoking map object.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. boolean equals(Object obj)<\/strong><\/span><\/p>\n<p>This method returns true if obj is a Map that contains the same entries in the calling map object else it returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. Object get(Object k)<\/strong><\/span><\/p>\n<p>This method returns the value associated with the key k.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. int hashCode( )<\/strong><\/span><\/p>\n<p>This method returns the hash code for the invoking map.<\/p>\n<p><span style=\"color: #008000;\"><strong>8. boolean isEmpty( )<\/strong><\/span><\/p>\n<p>This method returns true if the invoking map object is empty else it returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. Set keySet( )<\/strong><\/span><\/p>\n<p>This method returns a Set that contains the keys in the invoking map. This method provides a set view of the keys in the calling map.<\/p>\n<p><span style=\"color: #008000;\"><strong>10. Object put(Object k, Object v)<\/strong><\/span><\/p>\n<p>This method puts an entry in the calling map that overwrites any previous value associated with the key, if associated.<\/p>\n<p><span style=\"color: #008000;\"><strong>11. void putAll(Map m)<\/strong><\/span><\/p>\n<p>This method puts all the entries from Map m into the calling map.<\/p>\n<p><span style=\"color: #008000;\"><strong>12. Object remove(Object k)<\/strong><\/span><\/p>\n<p>This method removes the entry whose key equals k.<\/p>\n<p><span style=\"color: #008000;\"><strong>13. int size( )<\/strong><\/span><\/p>\n<p>This method returns the number of entries in the map.<\/p>\n<p><span style=\"color: #008000;\"><strong>14. Collection values( )<\/strong><\/span><\/p>\n<p>This method Returns a collection that contains the values in the map. This method returns a collection view of the values in the map.<\/p>\n<hr \/>\n<h4><strong>Map Important Points In JAVA<\/strong><\/h4>\n<ul>\n<li>Map has three implementation classes HashMap, LinkedHashMap and TreeMap. If we have requirement of storing elements in natural order than we should use TreeMap and if we have requirement of retrieving the elements as it is entered than we should use LinkedHashMap, and if no such requirements are there we can use HashMap.<\/li>\n<li>Duplicate keys are not allowed.<\/li>\n<li>Key value pair is known as Entry<\/li>\n<li>The Map interface do not Extends the Collectioninterface<\/li>\n<li>We cannot Iterate over the Entries of the map using Iterator or ListIterator.<\/li>\n<\/ul>\n<hr \/>\n<h4><u><\/u><strong>Map Interface Quick Revision Points<\/strong><\/h4>\n<ul>\n<li>Duplicates keys are not allowed<\/li>\n<li>TreeMap is preferred where we want to store data in natural order<\/li>\n<li>LinkedHashMap is preferred where we want to maintain insertion order.<\/li>\n<li>HashMap is preferred where we just want to store data without any requirement of maintaining any special ordering<\/li>\n<li>It is not part of collection Interface<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0Map\u00a0Interface is the mapping between a key and a value i.e. it contains values based on the key. Each key value pair is often known as an entry, As Map contains only unique keys, we can also say Map interface maps unique keys to its corresponding values. With the key we can retrieve a value &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/map\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Map Interface in Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1180","page","type-page","status-publish","hentry"],"psp_head":"<title>Map Interface in Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"The Map Interface is the mapping between a key and a value. Each key value pair is often known as an entry, As Map contains only unique keys, we can also say Map interface maps unique keys to its corresponding values.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/map\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1180","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=1180"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1180\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}