{"id":1274,"date":"2022-02-26T07:31:12","date_gmt":"2022-02-26T07:31:12","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1274"},"modified":"2022-02-26T07:31:12","modified_gmt":"2022-02-26T07:31:12","slug":"linked-hash-map","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/linked-hash-map\/","title":{"rendered":"Linked Hash Map"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java LinkedHashMap class and its operations with the help of examples.<\/p>\n\n\n\n<p>The\u00a0<code>LinkedHashMap<\/code>\u00a0class of the Java collections framework provides the hash table and linked list implementation of the\u00a0<a href=\"https:\/\/www.programiz.com\/java-programming\/map\">Map <\/a>i<a href=\"https:\/\/www.programiz.com\/java-programming\/map\">nterface<\/a>.<\/p>\n\n\n\n<p>The\u00a0<code>LinkedHashMap<\/code>\u00a0interface extends the\u00a0HashMap\u00a0class to store its entries in a hash table. It internally maintains a doubly-linked list among all of its entries to order its entries.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-linkedhashmap.png\" alt=\"Java LinkedHashMap class extends the HashMap class.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Creating a LinkedHashMap<\/h2>\n\n\n\n<p>In order to create a linked hashmap, we must import the&nbsp;<code>java.util.LinkedHashMap<\/code>&nbsp;package first. Once we import the package, here is how we can create linked hashmaps in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LinkedHashMap with initial capacity 8 and load factor 0.6\nLinkedHashMap&lt;Key, Value&gt; numbers = new LinkedHashMap&lt;&gt;(8, 0.6f);\n<\/code><\/pre>\n\n\n\n<p>In the above code, we have created a linked hashmap named&nbsp;<var>numbers<\/var>.<\/p>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><var>Key<\/var>&nbsp;&#8211; a unique identifier used to associate each element (value) in a map<\/li><li><var>Value<\/var>&nbsp;&#8211; elements associated by the keys in a map<\/li><\/ul>\n\n\n\n<p>Notice the part&nbsp;<code>new LinkedHashMap&lt;&gt;(8, 0.6)<\/code>. Here, the first parameter is&nbsp;<strong>capacity<\/strong>&nbsp;and the second parameter is&nbsp;<strong>loadFactor<\/strong>.<\/p>\n\n\n\n<ul><li><strong>capacity<\/strong>&nbsp;&#8211; The capacity of this linked hashmap is 8. Meaning, it can store 8 entries.<\/li><li><strong>loadFactor<\/strong>&nbsp;&#8211; The load factor of this linked hashmap is 0.6. This means, whenever our hash map is filled by 60%, the entries are moved to a new hash table of double the size of the original hash table.<\/li><\/ul>\n\n\n\n<p><strong>Default capacity and load factor<\/strong><\/p>\n\n\n\n<p>It&#8217;s possible to create a linked hashmap without defining its capacity and load factor. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/LinkedHashMap with default capacity and load factor\nLinkedHashMap&lt;Key, Value&gt; numbers1 = new LinkedHashMap&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>By default,<\/p>\n\n\n\n<ul><li>the capacity of the linked hashmap will be 16<\/li><li>the load factor will be 0.75<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: The&nbsp;<code>LinkedHashMap<\/code>&nbsp;class also allows us to define the order of its entries. For example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LinkedHashMap with specified order\nLinkedHashMap&lt;Key, Value&gt; numbers2 = new LinkedHashMap&lt;&gt;(capacity, loadFactor, accessOrder);\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>accessOrder<\/var>&nbsp;is a boolean value. Its default value is&nbsp;<code>false<\/code>. In this case entries in the linked hashmap are ordered on the basis of their insertion order.<\/p>\n\n\n\n<p>However, if&nbsp;<code>true<\/code>&nbsp;is passed as&nbsp;<var>accessOrder<\/var>, entries in the linked hashmap will be ordered from least-recently accessed to most-recently accessed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating LinkedHashMap from Other Maps<\/h2>\n\n\n\n<p>Here is how we can create a linked hashmap containing all the elements of other maps.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashMap;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        \/\/ Creating a LinkedHashMap of even numbers\n        LinkedHashMap&lt;String, Integer&gt; evenNumbers = new LinkedHashMap&lt;&gt;();\n        evenNumbers.put(\"Two\", 2);\n        evenNumbers.put(\"Four\", 4);\n        System.out.println(\"LinkedHashMap1: \" + evenNumbers);\n\n        \/\/ Creating a LinkedHashMap from other LinkedHashMap\n        LinkedHashMap&lt;String, Integer&gt; numbers = new LinkedHashMap&lt;&gt;(evenNumbers);\n        numbers.put(\"Three\", 3);\n        System.out.println(\"LinkedHashMap2: \" + numbers);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedHashMap1: {Two=2, Four=4}\nLinkedHashMap2: {Two=2, Four=4, Three=3}\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of LinkedHashMap<\/h2>\n\n\n\n<p>The&nbsp;<code>LinkedHashMap<\/code>&nbsp;class provides methods that allow us to perform various operations on the map.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"insert\">Insert Elements to LinkedHashMap<\/h2>\n\n\n\n<ul><li><code>put()<\/code>&nbsp;&#8211; inserts the specified key\/value mapping to the map<\/li><li><code>putAll()<\/code>&nbsp;&#8211; inserts all the entries from the specified map to this map<\/li><li><code>putIfAbsent()<\/code>&nbsp;&#8211; inserts the specified key\/value mapping to the map if the specified key is not present in the map<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport java.util.LinkedHashMap;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        \/\/ Creating LinkedHashMap of even numbers\n        LinkedHashMap&lt;String, Integer&gt; evenNumbers = new LinkedHashMap&lt;&gt;();\n\n        \/\/ Using put()\n        evenNumbers.put(\"Two\", 2);\n        evenNumbers.put(\"Four\", 4);\n        System.out.println(\"Original LinkedHashMap: \" + evenNumbers);\n\n        \/\/ Using putIfAbsent()\n        evenNumbers.putIfAbsent(\"Six\", 6);\n        System.out.println(\"Updated LinkedHashMap(): \" + evenNumbers);\n\n        \/\/Creating LinkedHashMap of numbers\n        LinkedHashMap&lt;String, Integer&gt; numbers = new LinkedHashMap&lt;&gt;();\n        numbers.put(\"One\", 1);\n\n        \/\/ Using putAll()\n        numbers.putAll(evenNumbers);\n        System.out.println(\"New LinkedHashMap: \" + numbers);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Original LinkedHashMap: {Two=2, Four=4}\nUpdated LinkedHashMap: {Two=2, Four=4, Six=6}\nNew LinkedHashMap: {One=1, Two=2, Four=4, Six=6}\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access\">Access LinkedHashMap Elements<\/h2>\n\n\n\n<p><strong>1. Using entrySet(), keySet() and values()<\/strong><\/p>\n\n\n\n<ul><li><code>entrySet()<\/code>&nbsp;&#8211; returns a set of all the key\/value mapping of the map<\/li><li><code>keySet()<\/code>&nbsp;&#8211; returns a set of all the keys of the map<\/li><li><code>values()<\/code>&nbsp;&#8211; returns a set of all the values of the map<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashMap;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashMap&lt;String, Integer&gt; numbers = new LinkedHashMap&lt;&gt;();\n\n        numbers.put(\"One\", 1);\n        numbers.put(\"Two\", 2);\n        numbers.put(\"Three\", 3);\n        System.out.println(\"LinkedHashMap: \" + numbers);\n\n        \/\/ Using entrySet()\n        System.out.println(\"Key\/Value mappings: \" + numbers.entrySet());\n\n        \/\/ Using keySet()\n        System.out.println(\"Keys: \" + numbers.keySet());\n\n        \/\/ Using values()\n        System.out.println(\"Values: \" + numbers.values());\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedHashMap: {One=1, Two=2, Three=3}\nKey\/Value mappings: &#91;One=1, Two=2, Three=3]\nKeys: &#91;One, Two, Three]\nValues: &#91;1, 2, 3]\n<\/samp><\/code><\/pre>\n\n\n\n<p><strong>2. Using get() and getOrDefault()<\/strong><\/p>\n\n\n\n<ul><li><code>get()<\/code>&nbsp;&#8211; Returns the value associated with the specified key. If the key is not found, it returns&nbsp;<code>null<\/code>.<\/li><li><code>getOrDefault()<\/code>&nbsp;&#8211; Returns the value associated with the specified key. If the key is not found, it returns the specified default value.<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashMap;\n\nclass Main {\n    public static void main(String&#91;] args) {\n\n        LinkedHashMap&lt;String, Integer&gt; numbers = new LinkedHashMap&lt;&gt;();\n        numbers.put(\"One\", 1);\n        numbers.put(\"Two\", 2);\n        numbers.put(\"Three\", 3);\n        System.out.println(\"LinkedHashMap: \" + numbers);\n\n        \/\/ Using get()\n        int value1 = numbers.get(\"Three\");\n        System.out.println(\"Returned Number: \" + value1);\n\n        \/\/ Using getOrDefault()\n        int value2 = numbers.getOrDefault(\"Five\", 5);\n        System.out.println(\"Returned Number: \" + value2);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedHashMap: {One=1, Two=2, Three=3}\nReturned Number: 3\nReturned Number: 5\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"remove\">Removed LinkedHashMap Elements<\/h2>\n\n\n\n<ul><li><code>remove(key)<\/code>&nbsp;&#8211; returns and removes the entry associated with the specified&nbsp;<var>key<\/var>&nbsp;from the map<\/li><li><code>remove(key, value)<\/code>&nbsp;&#8211; removes the entry from the map only if the specified&nbsp;<var>key<\/var>&nbsp;mapped to be the specified&nbsp;<var>value<\/var>&nbsp;and return a boolean value<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashMap;\n\nclass Main {\n    public static void main(String&#91;] args) {\n\n        LinkedHashMap&lt;String, Integer&gt; numbers = new LinkedHashMap&lt;&gt;();\n        numbers.put(\"One\", 1);\n        numbers.put(\"Two\", 2);\n        numbers.put(\"Three\", 3);\n        System.out.println(\"LinkedHashMap: \" + numbers);\n\n        \/\/ remove method with single parameter\n        int value = numbers.remove(\"Two\");\n        System.out.println(\"Removed value: \" + value);\n\n        \/\/ remove method with two parameters\n        boolean result = numbers.remove(\"Three\", 3);\n        System.out.println(\"Is the entry Three removed? \" + result);\n\n        System.out.println(\"Updated LinkedHashMap: \" + numbers);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedHashMap: {One=1, Two=2, Three=3}\nRemoved value: 2\nIs the entry {Three=3} removed? True\nUpdated LinkedHashMap: {One=1}\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Other Methods of LinkedHashMap<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>clear()<\/code><\/td><td>removes all the entries from the map<\/td><\/tr><tr><td><code>containsKey()<\/code><\/td><td>checks if the map contains the specified key and returns a boolean value<\/td><\/tr><tr><td><code>containsValue()<\/code><\/td><td>checks if the map contains the specified value and returns a boolean value<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>returns the size of the map<\/td><\/tr><tr><td><code>isEmpty()<\/code><\/td><td>checks if the map is empty and returns a boolean value<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"vs-hashmap\">LinkedHashMap Vs. HashMap<\/h2>\n\n\n\n<p>Both the&nbsp;<code>LinkedHashMap<\/code>&nbsp;and the&nbsp;<code>HashMap<\/code>&nbsp;implements the&nbsp;<code>Map<\/code>&nbsp;interface. However, there exist some differences between them.<\/p>\n\n\n\n<ul><li><code>LinkedHashMap<\/code>&nbsp;maintains a doubly-linked list internally. Due to this, it maintains the insertion order of its elements.<\/li><li>The&nbsp;<code>LinkedHashMap<\/code>&nbsp;class requires more storage than&nbsp;<code>HashMap<\/code>. This is because&nbsp;<code>LinkedHashMap<\/code>&nbsp;maintains linked lists internally.<\/li><li>The performance of&nbsp;<code>LinkedHashMap<\/code>&nbsp;is slower than&nbsp;<code>HashMap<\/code>.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java LinkedHashMap class and its operations with the help of examples. The\u00a0LinkedHashMap\u00a0class of the Java collections framework provides the hash table and linked list implementation of the\u00a0Map interface. The\u00a0LinkedHashMap\u00a0interface extends the\u00a0HashMap\u00a0class to store its entries in a hash table. It internally maintains a doubly-linked list among all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[398],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1274"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1274"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1274\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}