{"id":1045,"date":"2016-03-08T06:30:42","date_gmt":"2016-03-08T06:30:42","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1045"},"modified":"2018-06-05T06:34:18","modified_gmt":"2018-06-05T06:34:18","slug":"linkedhashmap","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/linkedhashmap","title":{"rendered":"LinkedHashMap Tutorial In Java With Example"},"content":{"rendered":"<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. The pair of key and value is often known as Entry. As LinkedHashMap is sub-class of HashMap class so it also indirectly extends AbstractMap class and which further implements Map Interface.<\/p>\n<p>In Addition to all the functionalities of HashMap Class, the functionality of maintaining the insertion is added into LinkedHashMap and 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 the map.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of <\/strong><strong>Linked<\/strong><strong>HashMap class<\/strong><\/h4>\n<p>LinkedHashMap Class extends HashMap Class which further \u00a0extends AbstractMap Class and that Implements Map Interface as shown in following\u00a0 figure 1.<\/p>\n<p>Dotted arrow shows AbstractMap implements Map Interface and Bold Array shows \u00a0LinkedHashMap extends HashMap which further extends AbstractMap class.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1061\" src=\"\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-LinkedHashMap-class.png\" alt=\"Hierarchy of LinkedHashMap class\" width=\"258\" height=\"408\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-LinkedHashMap-class.png 358w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-LinkedHashMap-class-190x300.png 190w\" sizes=\"auto, (max-width: 258px) 100vw, 258px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>\u00a0LinkedHashMap Methods in JAVA<\/strong><\/h4>\n<p>Let us discuss all the LinkedHashMap methods one by one with Examples in Java.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. void clear()<\/strong><\/span><\/p>\n<p>This method as the name suggests removes all the entries in the LinkedHashMap, as shown in the following program<\/p>\n<pre>import java.util.LinkedHashMap;\r\n\r\nimport java.util.Map;\r\n\r\npublic class LinkedHashMapDemo {\r\n\r\npublic static void main(String[] args) {\r\n\r\nMap&lt;String, Integer&gt; linkedHashMapobject = new LinkedHashMap&lt;String, Integer&gt;();\r\n\r\nlinkedHashMapobject.put(\"Samsung Grand quattro price \", new Integer(10000));\r\n\r\nlinkedHashMapobject.put(\"Micromax canvas price\", new Integer(9000));\r\n\r\nlinkedHashMapobject.put(\"Sony T2 Ultra price\", new Integer(20000));\r\n\r\nlinkedHashMapobject.put(\"Nokia Lumia price\", new Integer(15000));\r\n\r\nlinkedHashMapobject.put(\"Microsoft Lumia price \", new Integer(16000));\r\n\r\n\/\/ Displaying the contents of the LinkedHashMap before usig clear method\r\n\r\nSystem.out.println(\"Contents of LinkedHashMap before clear method : \" +\r\n\r\nlinkedHashMapobject);\r\n\r\nlinkedHashMapobject.clear();\r\n\r\n\/\/Displaying the contents of the LinkedHashMap before usig clear method\r\n\r\nSystem.out.println(\"Contents of LinkedHashMap\u00a0 after clear method: \" +\r\n\r\nlinkedHashMapobject);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Contents of LinkedHashMap before clear method: {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}\r\n\r\nContents of LinkedHashMap after clear method: {}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. boolean containsKey(Object key)<\/strong><\/span><\/p>\n<p>This methods checks whether the key given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program .<\/p>\n<pre>import java.util.LinkedHashMap;\r\n\r\nimport java.util.Map;\r\n\r\npublic class LinkedHashMapDemo {\r\n\r\npublic static void main(String[] args) {\r\n\r\nMap&lt;String, Integer&gt; linkedHashMapobject = new LinkedHashMap&lt;String, Integer&gt;();\r\n\r\nlinkedHashMapobject.put(\"Samsung Grand quattro price \", new Integer(10000));\r\n\r\nlinkedHashMapobject.put(\"Micromax canvas price\", new Integer(9000));\r\n\r\nlinkedHashMapobject.put(\"Sony T2 Ultra price\", new Integer(20000));\r\n\r\nlinkedHashMapobject.put(\"Nokia Lumia price\", new Integer(15000));\r\n\r\nlinkedHashMapobject.put(\"Microsoft Lumia price \", new Integer(16000));\r\n\r\n\/\/ Displaying the contents of the LinkedHashMap before <u>usig<\/u> clear method\r\n\r\nSystem.<em>out<\/em>.println(\"Contents of LinkedHashMap before : \" + linkedHashMapobject);\r\n\r\n\/\/Checking whether Map contains a particular key\r\n\r\nSystem.<em>out<\/em>.println(\"\\nLinkedHashMap contains 'Nokia Lumia price' as key? : \" + linkedHashMapobject.containsKey(\"Nokia Lumia price\"));\r\n}\r\n}\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Output:<\/span><\/strong><\/p>\n<pre>Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}\r\n\r\nLinkedHashMap contains 'Nokia Lumia price' as key? : true\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. boolean containsValue(Object value)<\/strong><\/span><\/p>\n<p>This methods checks whether the value given in the argument list is present or not in the LinkedHashMap entries, as shown in the following program.<\/p>\n<pre>import java.util.LinkedHashMap;\r\n\r\nimport java.util.Map;\r\n\r\npublic class LinkedHashMapDemo {\r\n\r\npublic static void main(String[] args) {\r\n\r\nMap&lt;String, Integer&gt; linkedHashMapobject = new LinkedHashMap&lt;String, Integer&gt;();\r\n\r\nlinkedHashMapobject.put(\"Samsung Grand quattro price \", new Integer(10000));\r\n\r\nlinkedHashMapobject.put(\"Micromax canvas price\", new Integer(9000));\r\n\r\nlinkedHashMapobject.put(\"Sony T2 Ultra price\", new Integer(20000));\r\n\r\nlinkedHashMapobject.put(\"Nokia Lumia price\", new Integer(15000));\r\n\r\nlinkedHashMapobject.put(\"Microsoft Lumia price \", new Integer(16000));\r\n\r\n\/\/ Displaying the contents of the LinkedHashMap before <u>usig<\/u> clear method\r\n\r\nSystem.<em>out<\/em>.println(\"Contents of LinkedHashMap before : \" + linkedHashMapobject);\r\n\r\n\/\/Checking whether Map contains a particular value\r\n\r\nSystem.<em>out<\/em>.println(\"LinkedHashMap contains 16000 as value? : \" + linkedHashMapobject.containsValue(16000));\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Contents of LinkedHashMap before : {Samsung Grand quattro price =10000, Micromax canvas price=9000, Sony T2 Ultra price=20000, Nokia Lumia price=15000, Microsoft Lumia price =16000}\r\n\r\nLinkedHashMap contains 16000 as value? : true\r\n<\/pre>\n<p><strong>Read All <a href=\"\/java\/linkedhashmap-methods.html\">LinkedHashMap\u00a0Methods in JAVA with Example<\/a><\/strong><\/p>\n<hr \/>\n<h4><strong>Example of LinkedHashMap class<\/strong><\/h4>\n<p>Let us discuss Linked Hashmap class with the help of program, following program has been divided into 3 Steps that we will discuss one by one.<\/p>\n<pre>import java.util.LinkedHashMap;\r\n\r\nimport java.util.Map;\r\n\r\npublic class LinkedHashMapDemo {\r\n\r\npublic static void main(String[] args) {\r\n\r\nMap&lt;String, Integer&gt; linkedHashMapobject = new LinkedHashMap&lt;String, Integer&gt;();\/\/ Step 1\r\n\r\nlinkedHashMapobject.put(\"Samsung Grand quattro price \", new Integer(10000)); \/\/ Step 2\r\n\r\nlinkedHashMapobject.put(\"Micromax canvas price\", new Integer(9000));\r\n\r\nlinkedHashMapobject.put(\"Sony T2 Ultra price\", new Integer(20000));\r\n\r\nlinkedHashMapobject.put(\"Nokia Lumia price\", new Integer(15000));\r\n\r\nlinkedHashMapobject.put(\"Microsoft Lumia price \", new Integer(16000));\r\n\r\nSystem.out.println(\"Contents of LinkedHashMap : \" + linkedHashMapobject); \/\/step 3 \r\n\r\nSystem.out.println(\"\\nValues of linkedHashMapobject after iterating over it : \\n\");\r\n\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 \/\/Step 4\r\n\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>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><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\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 15000\r\n\r\nMicrosoft Lumia price :\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 16000<\/pre>\n<hr \/>\n<h4><u><\/u><strong>Important Points About LinkedHashMap<\/strong><\/h4>\n<ul>\n<li>For using LinkedHashMap you must need to import util.LinkedHashMap or its super class.<\/li>\n<li>LinkedHashMap only stores Unique values, that is duplicate values are not allowed.<\/li>\n<li>LinkedHashMap maintains order, which means it returns the key value pair in the order in which they have been added.<\/li>\n<li>LinkedHashMap similar to 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>If a key is re-inserted into the map, insertion order of LinkedHashMap object is not affected at all.<\/li>\n<li>Due to the added expense of maintaining the linked list performance of LinkedHashMap is slightly below than that of HashMap,<\/li>\n<li>LinkedHashMap likewise HashMap is not thread Safe, That is, it is also unsynchronized.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>LinkedHashMap Quick Revision Points<\/strong><\/h4>\n<ul>\n<li>Data is stores in key value pair<\/li>\n<li>Only unique values are allowed<\/li>\n<li>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<\/ul>\n","protected":false},"excerpt":{"rendered":"<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. The pair of key and value is often known as Entry. As LinkedHashMap is sub-class of HashMap class so it also indirectly extends AbstractMap class &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/linkedhashmap\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">LinkedHashMap 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-1045","page","type-page","status-publish","hentry"],"psp_head":"<title>LinkedHashMap Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class, In Addition, the functionality of maintaining the insertion is added into LinkedHashMap\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/linkedhashmap\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1045","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=1045"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1045\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1045\/revisions\/1481"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}