{"id":1050,"date":"2016-03-08T06:01:15","date_gmt":"2016-03-08T06:01:15","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1050"},"modified":"2016-03-08T06:24:17","modified_gmt":"2016-03-08T06:24:17","slug":"linkedhashmap-methods","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/linkedhashmap-methods.html","title":{"rendered":"LinkedHashMap Methods Tutorial With Examples in JAVA"},"content":{"rendered":"<p>LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class. In Addition to them, the functionality of maintaining the insertion is added into LinkedHashMap and to attain this 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<hr \/>\n<h4><strong>LinkedHashMap 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><span style=\"color: #008000;\"><strong>4. Object get(Object key)<\/strong><\/span><\/p>\n<p>This method returns the value corresponding to the given key in the argument list , as shown in the following program.<strong>\u00a0<\/strong><\/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\nSystem.<em>out<\/em>.println(\"Price of Samsung Grand quattro::\" +linkedHashMapobject.get(\"Samsung Grand quattro price\"));\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Price of Samsung Grand quattro ::10000\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. protected boolean removeEldestEntry(Map.Entry eldest)<\/strong><\/span><\/p>\n<p>This method as the name suggests, remove the eldest Entry from the LinkedHashMap.<\/p>\n<p>Now the question is when it should remove the eldest entry<\/p>\n<p>Let us consider a case when we have to store only 4 entries in our linkedHashMap.<\/p>\n<p>We will store 4 in a variable maximumEntries and now every time we try to put any entry into linkedHashMap this methods compares the size of our linkedHashMap to this value, If the value of size comes greater than the predefined size then this methods automatically \u00a0deletes the eldest element , as shown in the output of 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\nprivate static final int <em>maximumEntries<\/em>= 4;\r\n\r\npublic static void main(String[] args) {\r\n\r\nLinkedHashMap linkedHashMap = new LinkedHashMap(<em>maximumEntries<\/em>) {\r\n\r\nprotected boolean removeEldestEntry(Map.Entry eldest)\r\n\r\n{\r\n\r\nreturn size() &gt; <em>maximumEntries<\/em>;\r\n\r\n}\r\n\r\n};\r\n\r\nlinkedHashMap.put(0, \"I\");\r\n\r\nlinkedHashMap.put(1, \"L\");\r\n\r\nlinkedHashMap.put(2, \"O\");\r\n\r\nlinkedHashMap.put(3, \"V\");\r\n\r\nlinkedHashMap.put(4, \"E\");\r\n\r\nlinkedHashMap.put(5, \"J\");\r\n\r\nlinkedHashMap.put(6, \"A\");\r\n\r\nlinkedHashMap.put(7, \"V\");\r\n\r\nlinkedHashMap.put(8, \"A\");\r\n\r\nSystem.<em>out<\/em>.println(\"After using removeEldestEntry function:: \" + linkedHashMap);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>After using removeEldestEntry function:: {5=J, 6=A, 7=V, 8=A}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LinkedHashMap is a type of Collection, which takes all the functionalities of HashMap class. In Addition to them, the functionality of maintaining the insertion is added into LinkedHashMap and to attain this 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 &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/linkedhashmap-methods.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">LinkedHashMap Methods Tutorial With Examples in JAVA<\/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-1050","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-collections"],"psp_head":"<title>LinkedHashMap Methods Tutorial With Examples in JAVA \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 to them, the functionality of maintaining the insertion is added into LinkedHashMap Class\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/linkedhashmap-methods.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1050","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=1050"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"predecessor-version":[{"id":1459,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1050\/revisions\/1459"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}