{"id":643,"date":"2021-06-21T12:56:34","date_gmt":"2021-06-21T12:56:34","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/06\/21\/map-interface-in-java\/"},"modified":"2021-06-21T12:56:34","modified_gmt":"2021-06-21T12:56:34","slug":"map-interface-in-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/06\/21\/map-interface-in-java\/","title":{"rendered":"Map Interface in Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"643\" id=\"tve_editor\">\n<div class=\"thrv_wrapper tve-toc tve-elem-scroll tve-toc-expandable tcb-local-vars-root\" data-animation=\"slide\" data-bullet-style='{\"0\":\"tve-u-17399ff41d4\",\"1\":\"tve-u-17399ffc502\",\"2\":\"tve-u-17399ffedb7\"}' data-columns=\"1\" data-css=\"tve-u-17a2e9ac2ae\" data-ct=\"toc-60733\" data-ct-name=\"Table of Contents 13\" data-distribute=\"false\" data-element-name=\"Table of Contents\" data-headers=\"h1,h3,h5\" data-heading-style='{\"0\":\"tve-u-17a2e9ac2b6\",\"1\":\"tve-u-17a2e9ac2b7\",\"2\":\"tve-u-17a2e9ac2b8\"}' data-highlight=\"heading\" data-number-style='{\"0\":\"tve-u-17399fecc2c\",\"1\":\"tve-u-173dc8687ce\",\"2\":\"tve-u-173dc86929b\"}' data-numbering=\"none\" data-state-default=\"expanded\" data-state-default-d=\"expanded\" data-state-default-m=\"collapsed\" data-transition=\"slide\" style=\"\">\n<div class=\"thrive-colors-palette-config\" style=\"display: none !important\"><\/div>\n<div class=\"tve-toc-divider\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\">\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgb(217, 217, 217)\" data-style=\"tve_sep-1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<p><svg class=\"toc-icons\" style=\"position: absolute; width: 0; height: 0; overflow: hidden;\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-0\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-1\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><symbol data-id=\"icon-chevron_right-duotone\" id=\"toc-bullet-2\" viewbox=\"0 0 24 24\"><path d=\"M0 0h24v24H0V0z\" fill=\"none\"><\/path><path d=\"M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6-6-6z\"><\/path><\/symbol><\/svg><\/p>\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17a2e9ac2af\" style=\"\"><\/div>\n<div class=\"thrv_wrapper tve-toc-title tcb-icon-display reverse tve-no-dropzone tve-prevent-content-edit\" data-css=\"tve-u-17a2e9ac2b0\" style=\"\">\n<div class=\"tve-content-box-background\" style=\"\"><\/div>\n<div class=\"tve-cb\" style=\"\">\n<div class=\"tve-toc-title-icon\" data-icon-code=\"icon-chevron-down-solid\" style=\"\"><svg class=\"tcb-icon\" data-id=\"icon-chevron-down-solid\" data-name=\"\" viewbox=\"0 0 24 24\"><path d=\"M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z\"><\/path><\/svg><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve_no_icons\">\n<div class=\"tcb-plain-text\" data-css=\"tve-u-17a2e9ac2b1\" style=\"\">table of contents<\/div>\n<\/p><\/div>\n<\/div>\n<\/div>\n<div class=\"tve-cb tve-toc-content tve-prevent-content-edit\" style=\"\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\" data-css=\"tve-u-17a2e9ac2b2\" style=\"\">\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17a2e9ac2b3\" style=\"\"><\/div>\n<div class=\"tve-cb\"><\/div>\n<\/div>\n<div class=\"thrv_wrapper tve-toc-list tcb-no-delete tcb-no-save tcb-no-clone tve-no-dropzone\" data-css=\"tve-u-17a2e9ac2b4\" style=\"\">\n<div class=\"tve-content-box-background\" data-css=\"tve-u-17a2e9ac2b5\" style=\"\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"tve_ct_content tve_clearfix\">\n<div class=\"ct_column\">\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484031\" jump-animation=\"smooth\">What is Map interface in JAVA?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484032\" jump-animation=\"smooth\">How to add elements to a Map?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484033\" jump-animation=\"smooth\">How to remove element from Map?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484034\" jump-animation=\"smooth\">How can we modify an element in a Map?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484035\" jump-animation=\"smooth\">How do we iterate over map?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484036\" jump-animation=\"smooth\">What is LinkedHashMap?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484037\" jump-animation=\"smooth\">What is TreeMap and when should it be used?<\/a><\/div>\n<div class=\"thrv_wrapper tve-toc-heading tve-toc-heading-level1 tve_no_icons\" data-css=\"tve-u-17a2e9ac2b7\" data-element-name=\"Heading Level 2\" data-tag=\"H3\"><a class=\"tve-toc-anchor tve-jump-scroll\" href=\"#t-1624279484038\" jump-animation=\"smooth\">How to fetch all the keys &amp; all the values from a Map?<\/a><\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv-divider tve-vert-divider\" data-color-d=\"rgb(217, 217, 217)\" data-style=\"tve_sep-1\">\n<hr class=\"tve_sep tve_sep-1\" style=\"\"\/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">What is Map interface in JAVA?<\/h3>\n<p>The Map interface, which is part of java.util package, stores elements in key-value pair. A key is an object that we use to retrieve a value later. The keys must be unique, but the values may be duplicated. Some maps can accept a null key and null values(HashMap &amp; LinkedHashMap), others (TreeMap) cannot. Map is declared as shown below:<\/p>\n<p><strong><em>Interface Map&lt;K, V&gt;<\/em><\/strong><\/p>\n<p>Here, K specifies the type of keys, and V specifies the type of values.<\/p>\n<p>Although Maps are part of Collection Framework, these do not extend Collection interface. The reason is the mechanism to store object in Collection interface and Map interface is different. <\/p>\n<p>Since Map is an interface, we need implementing classes of Map interface (or child interface of Map) to create Map object. The most common classes which provide the concrete implementation of Map are HashMap, LinkedHashMap &amp; TreeMap.<\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<h3 class=\"\">How to add elements to a Map?<\/h3>\n<p>Let\u2019s use HashMap class to create a Map and see how we can add elements to it. We need to use put() method to add elements to a map. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2e9d1270\" style=\"\">\n<p data-css=\"tve-u-17a2e9d5cda\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; hashmap = new HashMap&lt;&gt;();<br \/>hashmap.put(1, &#8220;One&#8221;);<br \/>hashmap.put(2, &#8220;Two&#8221;);<br \/>hashmap.put(null, null);<br \/>System.out.println(hashmap);<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2e9d7a76\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>{null=null, 1=One, 2=Two}<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Explanation<\/strong><\/p>\n<p>Unlike other Collection classes, we need to provide both key &amp; value type (Integer &amp; String in the example) while declaring the Map.<\/p>\n<p>Also, notice that null is accepted as key as well as value.<\/p>\n<h3 class=\"\">How to remove element from Map?<\/h3>\n<p>We need to use remove() method and pass they key which we need to remove. When the key is removed the corresponding value is removed too. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2e9e5206\" style=\"\">\n<p data-css=\"tve-u-17a2e9e9531\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; hashmap = new HashMap&lt;&gt;();<br \/>hashmap.put(1, &#8220;One&#8221;);<br \/>hashmap.put(2, &#8220;Two&#8221;);<br \/>hashmap.put(3, &#8220;Three&#8221;);<br \/>System.out.println(hashmap);<br \/>hashmap.remove(3);<br \/>System.out.println(hashmap);<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2e9eafa3\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>{1=One, 2=Two, 3=Three}<br \/>{1=One, 2=Two}<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">How can we modify an element in a Map?<\/h3>\n<p>After adding the elements if we wish to change the element, it can be done by again by adding the element using the put() method. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2e9f7ce1\" style=\"\">\n<p data-css=\"tve-u-17a2e9fc6fc\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; hashmap = new HashMap&lt;&gt;();<br \/>hashmap.put(1, &#8220;One&#8221;);<br \/>hashmap.put(2, &#8220;Two&#8221;);<br \/>hashmap.put(3, &#8220;Three&#8221;);<br \/>System.out.println(hashmap);<br \/>hashmap.put(3, &#8220;Updated_Three&#8221;);<br \/>System.out.println(hashmap);<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2e9fe4e7\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>{1=One, 2=Two, 3=Three}<br \/>{1=One, 2=Two, 3=Updated_Three}<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Explanation<\/strong><\/p>\n<p>In the program above, we have updated the value of key 3 to Updated_Three by inserting it again in the map.<\/p>\n<h3 class=\"\">How do we iterate over map?<\/h3>\n<p>Since Map interface does not extend Collection interface, we cannot call iterate() method on a Map to iterate all the elements. <\/p>\n<p>We can use entrySet() method of Map interface which returns a collection view of the Map. <\/p>\n<p>The collection view is stored in an interface called Map.Entry. Map.Entry offers methods getKey() &amp; getValue(), which are used to get the keys and values of the Map. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2ea0f261\" style=\"\">\n<p data-css=\"tve-u-17a2ea133a7\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; hashmap = new HashMap&lt;&gt;();<br \/>hashmap.put(1, &#8220;One&#8221;);<br \/>hashmap.put(2, &#8220;Two&#8221;);<br \/>hashmap.put(3, &#8220;Three&#8221;);<br \/>for (Map.Entry &lt;Integer, String &gt; mapElement : hashmap.entrySet()) {<br \/>int key = mapElement.getKey();<br \/>String value = mapElement.getValue();<\/p>\n<p>System.out.println(key + &#8221; : &#8221; + value);<br \/>}<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2ea14e32\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>1 : One<br \/>2 : Two<br \/>3 : Three<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 class=\"\">What is LinkedHashMap?<\/h3>\n<p>LikedHashMap extends HashMap with additional feature of maintaining the insertion order. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2ea1d347\" style=\"\">\n<p data-css=\"tve-u-17a2ea21ec8\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; linkedhashmap = new LinkedHashMap&lt;&gt;();<br \/>linkedhashmap.put(1, &#8220;Sam&#8221;);<br \/>linkedhashmap.put(3, &#8220;John&#8221;);<br \/>linkedhashmap.put(2, &#8220;Terry&#8221;);<br \/>for (Map.Entry&lt;Integer, String&gt; mapElement : linkedhashmap.entrySet()) {<\/p>\n<p>System.out.println(mapElement.getKey() + &#8221; : &#8221; + mapElement.getValue());<br \/>}<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2ea23658\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>1 : Sam<br \/>3 : John<br \/>2 : Terry<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Explanation<br \/><\/strong><br \/>Since we used LinkedHashMap for concrete implementation, the output shows that the insertion order is maintained.<\/p>\n<h3 class=\"\">What is TreeMap and when should it be used?<\/h3>\n<p>TreeMap class implements NavigableMap interface, which extends SortedMap, which extends Map. <\/p>\n<p>The unique feature TreeMap provides is, it is sorted based on the keys. Let\u2019s see an example. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2ea307ed\" style=\"\">\n<p data-css=\"tve-u-17a2ea364a1\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; treemap = new TreeMap&lt;&gt;();<br \/>treemap.put(1, &#8220;Sam&#8221;);<br \/>treemap.put(3, &#8220;John&#8221;);<br \/>treemap.put(2, &#8220;Terry&#8221;);<br \/>treemap.put(5, &#8220;Alpha&#8221;);<br \/>treemap.put(4, &#8220;Charlie&#8221;);<br \/>for (Map.Entry&lt;Integer, String&gt; mapElement : treemap.entrySet()) {<\/p>\n<p>System.out.println(mapElement.getKey() + &#8221; : &#8221; + mapElement.getValue());<br \/>}<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2ea37ffd\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>1 : Sam<br \/>2 : Terry<br \/>3 : John<br \/>4 : Charlie<br \/>5 : Alpha<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Explanation<\/strong><\/p>\n<p>Even though the insertion order is random, TreeMap sorted the elements based on the key as shown in the output.<\/p>\n<h3 class=\"\">How to fetch all the keys &amp; all the values from a Map?<\/h3>\n<p>We can use keySet() and values() methods respectively, to get all the keys and values from a Map. <\/p>\n<p><strong>Example<\/strong><\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad\">\n<div class=\"tve-content-box-background\" style=\"background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)) !important; background-size: auto !important; background-position: 50% 50% !important; background-attachment: scroll !important; background-repeat: no-repeat !important; --background-image:linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5))  !important; --background-size:auto auto  !important; --background-position:50% 50%  !important; --background-attachment:scroll  !important; --background-repeat:no-repeat  !important;\"><\/div>\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv_text_element\" data-css=\"tve-u-17a2ea4507b\" style=\"\">\n<p data-css=\"tve-u-17a2ea4b7b6\" style=\"\">import java.util.*;<\/p>\n<p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>Map&lt;Integer, String&gt; treemap = new TreeMap&lt;&gt;();<br \/>treemap.put(1, &#8220;Sam&#8221;);<br \/>treemap.put(3, &#8220;John&#8221;);<br \/>treemap.put(2, &#8220;Terry&#8221;);<br \/>treemap.put(5, &#8220;Alpha&#8221;);<br \/>treemap.put(4, &#8220;Charlie&#8221;);<\/p>\n<p>Set&lt;Integer&gt; keys = treemap.keySet();<br \/>Collection&lt;String&gt; values = treemap.values();<\/p>\n<p>for (int x : keys) {<br \/>System.out.println(x);<br \/>}<\/p>\n<p>System.out.println();<br \/>for (String y : values) {<br \/>System.out.println(y);<br \/>}<br \/>}<br \/>}<\/p>\n<p><span data-css=\"tve-u-17a2ea4d162\" style=\"color: rgb(0, 255, 0);\">Output<\/p>\n<p>1<br \/>2<br \/>3<br \/>4<br \/>5<\/p>\n<p>Sam<br \/>Terry<br \/>John<br \/>Charlie<br \/>Alpha<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>table of contents What is Map interface in JAVA? How to add elements to a Map? How to remove element from Map? How can we modify an element in a Map? How do we iterate over map? What is LinkedHashMap? What is TreeMap and when should it be used? How to fetch all the keys [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-java","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}