{"id":10334,"date":"2023-04-26T10:21:31","date_gmt":"2023-04-26T04:51:31","guid":{"rendered":"https:\/\/www.csestack.org\/?p=10334"},"modified":"2023-04-26T10:22:04","modified_gmt":"2023-04-26T04:52:04","slug":"java-treemap","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/java-treemap\/","title":{"rendered":"TreeMap in Java | Usage with Coding Examples"},"content":{"rendered":"\n<p>In Java, TreeMap is a class that implements the Map interface and provides a red-black tree-based implementation of a map.<\/p>\n\n\n\n<p>It stores data into key-value pairs in a sorted order that is based on the keys. This means that TreeMap maintains the order of the elements according to the natural order of their keys.<\/p>\n\n\n\n<p>Let\u2019s understand the working example of creating a TreeMap in Java.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fd66ecb4fad\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fd66ecb4fad\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.csestack.org\/java-treemap\/#How_to_Create_TreeMap_in_Java\" >How to Create TreeMap in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.csestack.org\/java-treemap\/#Creating_TreeMap_using_Comparator\" >Creating TreeMap using Comparator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.csestack.org\/java-treemap\/#Java_TreeMap_class_Methods\" >Java TreeMap class Methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.csestack.org\/java-treemap\/#Adding_Removing_and_Finding_TreeMap_Elements\" >Adding, Removing, and Finding TreeMap Elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.csestack.org\/java-treemap\/#Frequency_Counter_using_TreeMap\" >Frequency Counter using TreeMap<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.csestack.org\/java-treemap\/#Advantages_of_using_TreeMap_in_Java\" >Advantages of using TreeMap in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.csestack.org\/java-treemap\/#When_to_use_TreeMap_in_Java\" >When to use\u00a0TreeMap in Java?<\/a><\/li><\/ul><\/nav><\/div>\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_TreeMap_in_Java\"><\/span>How to Create TreeMap in Java<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.TreeMap;\npublic class MainClass {\n    public static void main(String[] args) {\n        \/\/ Creating a TreeMap\n        TreeMap&lt;String, Integer&gt; treeMap = new TreeMap&lt;&gt;();\n        \/\/ Adding elements to the TreeMap\n        treeMap.put(&quot;Rohan&quot;, 30);\n        treeMap.put(&quot;Sohan&quot;, 25);\n        treeMap.put(&quot;Mohan&quot;, 40);\n        treeMap.put(&quot;John&quot;, 35);\n        treeMap.put(&quot;Sorabh&quot;, 20);\n\n        \/\/ Displaying the TreeMap\n        System.out.println(&quot;TreeMap: &quot; + treeMap);\n    }\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TreeMap: {John=35, Mohan=40, Rohan=30, Sohan=25, Sorabh=20}<\/pre>\n\n\n\n<p>In the above example, we create a TreeMap and add some key-value pairs to it. We then display the TreeMap to the console.<\/p>\n\n\n\n<p>TreeMap sorts elements on the natural order of the keys, which is lexicographic order in the above example case.<\/p>\n\n\n\n<p>Let\u2019s understand creating TreeMap in other ways:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_TreeMap_using_Comparator\"><\/span>Creating TreeMap using Comparator<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>In this example, we create a TreeMap with a custom key order by passing a Comparator object that sorts the keys in reverse order to the TreeMap constructor.&nbsp;<\/p>\n\n\n\n<p>We then add some key-value pairs to the TreeMap and display the sorted TreeMap.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.Comparator;\nimport java.util.TreeMap;\n\nclass MainClass {\n\tpublic static void main(String[] args) {\n\n\t\t\/\/ Creating a TreeMap\n        TreeMap&lt;String, Integer&gt; treeMap = new TreeMap&lt;&gt;(Comparator.reverseOrder());\n\t\t\/\/ Adding elements to the TreeMap\n\t\ttreeMap.put(&quot;Rohan&quot;, 30);\n\t\ttreeMap.put(&quot;Sohan&quot;, 25);\n\t\ttreeMap.put(&quot;Mohan&quot;, 40);\n\t\ttreeMap.put(&quot;John&quot;, 35);\n\t\ttreeMap.put(&quot;Sorabh&quot;, 20);\n\n\t\t\/\/ Displaying the TreeMap\n        System.out.println(&quot;TreeMap with custom key order: &quot; + treeMap);\n\t}\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TreeMap with custom key order: {Sorabh=20, Sohan=25, Rohan=30, Mohan=40, John=35}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Java_TreeMap_class_Methods\"><\/span>Java TreeMap class Methods<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here are some of the commonly used methods of the Java TreeMap class:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">put(key, value)<\/pre>\n\n\n\n<p>It adds the specified key-value pair to the TreeMap. If the key already exists in the TreeMap, the new value replaces the existing one. It returns the previous value associated with the key, or null if there was no mapping for the key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">get(key)<\/pre>\n\n\n\n<p>It returns the value associated with the specified key in the TreeMap, or null if the key is not present in the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">remove(key)<\/pre>\n\n\n\n<p>It removes the key-value pair associated with the specified key from the TreeMap. Returns the value that was associated with the key, or null if the key is not present in the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">size()<\/pre>\n\n\n\n<p>It returns the number of key-value pairs in the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">clear()<\/pre>\n\n\n\n<p>It removes all key-value pairs from the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">containsKey(key)<\/pre>\n\n\n\n<p>It returns true if the TreeMap contains a key-value pair with the specified key, otherwise false.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">containsValue(value)<\/pre>\n\n\n\n<p>It returns true if the TreeMap contains a key-value pair with the specified value, otherwise false.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">firstKey()<\/pre>\n\n\n\n<p>It returns the first (lowest) key in the TreeMap, or null if the TreeMap is empty.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lastKey()<\/pre>\n\n\n\n<p>It returns the last (highest) key in the TreeMap, or null if the TreeMap is empty.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">keySet()<\/pre>\n\n\n\n<p>It returns a Set view of the keys contained in the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">values()<\/pre>\n\n\n\n<p>It returns a Collection view of the values contained in the TreeMap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">entrySet()<\/pre>\n\n\n\n<p>It returns a Set view of the key-value pairs contained in the TreeMap.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adding_Removing_and_Finding_TreeMap_Elements\"><\/span>Adding, Removing, and Finding TreeMap Elements<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here, we used <code>put()<\/code>, <code>remove()<\/code>, and <code>firstKey()<\/code> methods to find add, remove and get elements respectively.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.TreeMap;\n\nclass MainClass {\n\tpublic static void main(String[] args) {\n\n\t\t\/\/ Creating a TreeMap\n\t\tTreeMap&lt;String, Integer&gt; treeMap = new TreeMap&lt;&gt;();\n\t\t\/\/ Adding elements to the TreeMap\n\t\ttreeMap.put(&quot;Rohan&quot;, 30);\n\t\ttreeMap.put(&quot;Sohan&quot;, 25);\n\t\ttreeMap.put(&quot;Mohan&quot;, 40);\n\t\ttreeMap.put(&quot;John&quot;, 35);\n\t\ttreeMap.put(&quot;Sorabh&quot;, 20);\n\n\t\t\/\/ Displaying the TreeMap\n\t\tSystem.out.println(&quot;TreeMap: &quot; + treeMap);\n\t\t\n\t\t\/\/ Removing elements\n\t\ttreeMap.remove(&quot;Mohan&quot;);\n\t\t\n\t\t\/\/ Displaying the TreeMap\n\t\tSystem.out.println(&quot;TreeMap: &quot; + treeMap);\n\t\t\n\t\t\/\/ accessing the first key\n\t\tString val = treeMap.firstKey();\n\t\tSystem.out.println(val);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TreeMap: {John=35, Mohan=40, Rohan=30, Sohan=25, Sorabh=20}\nTreeMap: {John=35, Rohan=30, Sohan=25, Sorabh=20}\nJohn<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frequency_Counter_using_TreeMap\"><\/span>Frequency Counter using TreeMap<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Let\u2019s take another example of TreeMap to count the frequency of characters in the string.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.TreeMap;\n\nclass MainClass {\n\tpublic static void main(String[] args) {\n\n\t\t\/\/ Creating a TreeMap\n\t\tTreeMap&lt;Character, Integer&gt; frequencyMap = new TreeMap&lt;&gt;();\n\n\t\t\/\/ Counting the frequency of characters in a string\n\t\tString str = &quot;hello world dlrow&quot;;\n\t\tfor (int i = 0; i &lt; str.length(); i++) {\n\t\t\tchar c = str.charAt(i);\n\t\t\tif (frequencyMap.containsKey(c)) {\n\t\t\t\tfrequencyMap.put(c, frequencyMap.get(c) + 1);\n\t\t\t} else {\n\t\t\t\tfrequencyMap.put(c, 1);\n\t\t\t}\n\t\t}\n\n\t\t\/\/ Displaying the frequency map\n\t\tSystem.out.println(&quot;Frequency map: &quot; + frequencyMap);\n\t}\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Frequency map: { =1, d=1, e=1, h=1, l=3, o=2, r=1, w=1}<\/pre>\n\n\n\n<p>In this example, we use a TreeMap to count the frequency of characters in a string.&nbsp;<\/p>\n\n\n\n<p>We create a TreeMap with Character keys and Integer values to store the frequency count of each character in the input string.&nbsp;<\/p>\n\n\n\n<p>We then iterate over each character in the string, update the frequency count in the TreeMap, and finally display the frequency map.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advantages_of_using_TreeMap_in_Java\"><\/span>Advantages of using TreeMap in Java<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here are some advantages of using TreeMap in Java:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Ordered keys:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>TreeMap is implemented using a self-balancing binary search tree, which keeps the keys in sorted order. This allows for efficient range queries, such as finding all keys between two given values.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Fast lookup:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>Since the keys are stored in a binary search tree, lookups are efficient and can be performed in O(log n) time, where n is the number of keys in the map. This makes TreeMap a good choice for large datasets where lookup performance is important.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Custom sorting:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>TreeMap allows you to specify a custom Comparator to define the sorting order of the keys. This can be useful when you need to sort keys in a non-default order, such as sorting strings in reverse alphabetical order.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Key and value associations:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>TreeMap provides a way to associate a value with each key, allowing you to store and retrieve both keys and their associated values.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>Memory efficiency:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>TreeMap is implemented using a balanced binary search tree, which uses memory efficiently. The tree is balanced to ensure that the height of the tree is always logarithmic, which means that the memory used by the tree is proportional to the number of keys stored in the map.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>Thread safety:<\/strong> <\/li>\n<\/ol>\n\n\n\n<p>TreeMap is not thread-safe by default, but it can be made thread-safe by using appropriate synchronization techniques.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-left\"><span class=\"ez-toc-section\" id=\"When_to_use_TreeMap_in_Java\"><\/span><br>When to use\u00a0TreeMap in Java?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>TreeMap is a powerful data structure in Java that allows efficient storage and retrieval of key-value pairs. It provides fast lookups, ordered keys, custom sorting, and memory efficiency.<\/p>\n\n\n\n<p>It is useful for a variety of applications where key-value storage and retrieval are required, including sorting, searching, and indexing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.<\/p>\n","protected":false},"author":71,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,39],"tags":[67],"class_list":["post-10334","post","type-post","status-publish","format-standard","hentry","category-code","category-java","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>TreeMap in Java | Usage with Coding Examples<\/title>\n<meta name=\"description\" content=\"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/java-treemap\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TreeMap in Java | Usage with Coding Examples\" \/>\n<meta property=\"og:description\" content=\"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/java-treemap\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-26T04:51:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-26T04:52:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Irfan Khan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CSEStack\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Irfan Khan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/\"},\"author\":{\"name\":\"Irfan Khan\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/124ff85fb60794cf102c51fa158d4cf8\"},\"headline\":\"TreeMap in Java | Usage with Coding Examples\",\"datePublished\":\"2023-04-26T04:51:31+00:00\",\"dateModified\":\"2023-04-26T04:52:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/\"},\"wordCount\":802,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"keywords\":[\"Java\"],\"articleSection\":[\"Code\",\"JAVA\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/\",\"name\":\"TreeMap in Java | Usage with Coding Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2023-04-26T04:51:31+00:00\",\"dateModified\":\"2023-04-26T04:52:04+00:00\",\"description\":\"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/java-treemap\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TreeMap in Java | Usage with Coding Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/124ff85fb60794cf102c51fa158d4cf8\",\"name\":\"Irfan Khan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g\",\"caption\":\"Irfan Khan\"},\"description\":\"I\u2019m a software programmer having more than 5 years of development experience in Java and related technology. I write technical content as well and love to share the technical knowledge to make it available for all.\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/irfan\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TreeMap in Java | Usage with Coding Examples","description":"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/java-treemap\/","og_locale":"en_US","og_type":"article","og_title":"TreeMap in Java | Usage with Coding Examples","og_description":"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.","og_url":"https:\/\/www.csestack.org\/java-treemap\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2023-04-26T04:51:31+00:00","article_modified_time":"2023-04-26T04:52:04+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Irfan Khan","twitter_card":"summary_large_image","twitter_creator":"@CSEStack","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Irfan Khan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/java-treemap\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/java-treemap\/"},"author":{"name":"Irfan Khan","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/124ff85fb60794cf102c51fa158d4cf8"},"headline":"TreeMap in Java | Usage with Coding Examples","datePublished":"2023-04-26T04:51:31+00:00","dateModified":"2023-04-26T04:52:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/java-treemap\/"},"wordCount":802,"commentCount":0,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"keywords":["Java"],"articleSection":["Code","JAVA"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/java-treemap\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/java-treemap\/","url":"https:\/\/www.csestack.org\/java-treemap\/","name":"TreeMap in Java | Usage with Coding Examples","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2023-04-26T04:51:31+00:00","dateModified":"2023-04-26T04:52:04+00:00","description":"How to Create TreeMap in Java? Advantages and when to use TreeMap in Java? Java TreeMap class Methods. Adding, Removing, and Finding TreeMap Elements.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/java-treemap\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/java-treemap\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/java-treemap\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"TreeMap in Java | Usage with Coding Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"]},{"@type":"Person","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/124ff85fb60794cf102c51fa158d4cf8","name":"Irfan Khan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/930d6a83ec0db36c0fcc93402619e12e07051c35372cc24104d5925cdf70a762?s=96&d=monsterid&r=g","caption":"Irfan Khan"},"description":"I\u2019m a software programmer having more than 5 years of development experience in Java and related technology. I write technical content as well and love to share the technical knowledge to make it available for all.","url":"https:\/\/www.csestack.org\/author\/irfan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/10334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=10334"}],"version-history":[{"count":2,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/10334\/revisions"}],"predecessor-version":[{"id":10336,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/10334\/revisions\/10336"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=10334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=10334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=10334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}