{"id":1032,"date":"2016-03-09T05:48:08","date_gmt":"2016-03-09T05:48:08","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1032"},"modified":"2018-06-05T06:41:25","modified_gmt":"2018-06-05T06:41:25","slug":"treemap","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/treemap","title":{"rendered":"TreeMap Tutorial In Java With Example"},"content":{"rendered":"<p><u><\/u>TreeMap is a type of Collection that stores values based on the corresponding key. This key value pair is often called as Entry. It implements the NavigableMap interface and extends AbstractMap class.<\/p>\n<p>It can \u00a0stores only unique elements, that is duplicate values are not allowed and it cannot store key as <strong>null<\/strong> but is can\u00a0 store<strong> null <\/strong>values. It is mostly similar to HashMap and\u00a0key difference is it maintains an increasing order according to the key value.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of TreeMap class<\/strong><\/h4>\n<p>TreeMap Class extends AbstractMap Class and that Implements Map Interface, TreeMap also implements NavigableMap interface, which extends Sorted Map Interface, and it further extends Map Interface as shown in fiqure 1.<\/p>\n<p>Dotted arrow shows AbstractMap class implements Map Interface , and TreeMap class implements NavigableMap Interface<\/p>\n<p>Bold Array shows NavigableMap Interface extends SortedMap Interface which further extends Map Interface.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1040\" src=\"\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-TreeMap-class.png\" alt=\"Hierarchy of TreeMap class\" width=\"472\" height=\"362\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-TreeMap-class.png 736w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-TreeMap-class-300x230.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of TreeMap class<\/strong><\/h4>\n<p>Let us discuss Tree map with the help of program, following program has been divided into 4 Steps that we will discuss one by one.<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\u00a0\r\n\/\/Step 1:\r\nTreeMap&lt;Integer,String&gt; treemap=new TreeMap&lt;Integer,String&gt;();\r\n\/\/Step 2:\r\ntreemap.put(102,\"Core\");\r\n\r\ntreemap.put(100,\"I\");\r\n\r\ntreemap.put(103,\"Java\");\r\n\r\ntreemap.put(101,\"Love\");\r\n\r\n\/\/Step 3:\r\nfor(Map.Entry map\u00a0 : treemap.entrySet()){\u00a0 \r\n\/\/Step 4:\r\nSystem.<em>out<\/em>.println(map.getKey()+\" \"+map.getValue());\u00a0\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<ul>\n<li>In Step 1, we have created an object of Tree Map collection, We have defined key of Integer type and value of String type.<\/li>\n<li>In Step 2, we have used put method to add key value pair in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have used For Each loop to retrieve the values from the Tree Map. We have used method entrySet() which returns the Set containing all the keys and values<\/li>\n<li>In Step 4 we have used two methods , First is getKey () , which as name suggest retrieves the key and secondly getValue () method that retrieves the values corresponding to the keys.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>100 I\r\n\r\n101 Love\r\n\r\n102 Core\r\n\r\n103 Java\r\n<\/pre>\n<p>In output,We have obtained output in ascending order based on the Key, which distinguishes it from hash map<\/p>\n<hr \/>\n<h4><span id=\"LinkedList_Methods_In_JAVA\"><strong>TreeMap Methods In JAVA<\/strong><\/span><\/h4>\n<p>Let us discuss TreeMap\u00a0methods one by one with Examples in Java.<\/p>\n<p><span style=\"color: #008000;\"><strong>1 .\u00a0Object \u00a0ceilingEntry(Object key)<\/strong><\/span><\/p>\n<p>This method returns a Entry with the \u00a0least key greater than or equal to the given key in the argument list, or null if there is no such key, as shown in the following program<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\u00a0\r\nTreeMap&lt;Integer,String&gt; tm=new TreeMap&lt;Integer,String&gt;();\r\n\r\ntm.put(98,\"Core\");\r\n\r\ntm.put(100,\"I\");\r\n\r\ntm.put(103,\"Java\");\r\n\r\ntm.put(101,\"Love\");\r\n\u00a0\r\nSystem.<em>out<\/em>.println(\"Ceiling entry for 99: \"+ tm.ceilingEntry(99));\r\n\r\nSystem.<em>out<\/em>.println(\"Ceiling entry for 103: \"+ tm.ceilingEntry(103));\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Ceiling entry for 99: 100=I\r\n\r\nCeiling entry for 103: 103=Java\r\n<\/pre>\n<p>In the output we see ceiling value of key \u201c99\u201d is \u201c100\u201d as we see 99 is not present in treemap, it automatically tool least greater than \u201c99\u201d which is \u201c100\u201d.\u00a0And Ceiling value of \u201c103\u201d is \u201c103\u201d, which we see is present in the given map.<\/p>\n<p><span style=\"color: #008000;\"><strong>2.\u00a0Object ceilingKey(Object key)<\/strong><\/span><\/p>\n<p>This method returns a key with the\u00a0 least key greater than or equal to the given key in the argument list, or null if there is no such key, This method is same as ceilingEntry(Object key) , only difference is it returns only key, as shown in the following program<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\u00a0\r\nTreeMap&lt;Integer,String&gt; tm=new TreeMap&lt;Integer,String&gt;();\r\n\r\ntm.put(98,\"Core\");\r\n\r\ntm.put(100,\"I\");\r\n\r\ntm.put(103,\"Java\");\r\n\r\ntm.put(101,\"Love\");\r\n\r\nSystem.<em>out<\/em>.println(\"Ceiling key for 99: \"+ tm.ceilingKey(99));\r\n\r\nSystem.<em>out<\/em>.println(\"Ceiling key for 103: \"+ tm.ceilingKey(103));\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Ceiling key for 99: 100\r\n\r\nCeiling key for 103: 103\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3.\u00a0void clear()<\/strong><\/span><\/p>\n<p>This method removes all the entries from the TreeMap object as shown<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\u00a0\r\nTreeMap&lt;Integer,String&gt; tm=new TreeMap&lt;Integer,String&gt;();\r\n\r\ntm.put(102,\"Core\");\r\n\r\ntm.put(100,\"I\");\r\n\r\ntm.put(103,\"Java\");\r\n\r\ntm.put(101,\"Love\");\r\n\r\nSystem.<em>out<\/em>.println(\"TreeMap before : \" + tm);\r\n\r\ntm.clear();\r\n\r\nSystem.<em>out<\/em>.println(\"TreeMap After clear method: \" + tm);\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>TreeMap before : {100=I, 101=Love, 102=Core, 103=Java}\r\n\r\nTreeMap After clear method: {}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4.\u00a0Object clone()<\/strong><\/span><\/p>\n<p>This method returns a duplicate copy of the TreeMap object, as shown in the following program<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreeMapDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\nTreeMap&lt;Integer,String&gt; tm1 =new TreeMap&lt;Integer,String&gt;();\r\n\r\nTreeMap&lt;Integer, String&gt; tm2 = new TreeMap&lt;Integer, String&gt;();\r\n\r\ntm1.put(102,\"Core\");\r\n\r\ntm1.put(100,\"I\");\r\n\r\ntm1.put(103,\"Java\");\r\n\r\ntm1.put(101,\"Love\");\r\n\r\n\/\/using clone method\r\ntm2 =(TreeMap) tm1.clone();\r\n\r\nSystem.<em>out<\/em>.println(\"Before using Clone method: \"+ tm1);\r\n\r\nSystem.<em>out<\/em>.println(\"After using Clone method: \"+ tm2);\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Cloning tree map\r\n\r\nBefore using Clone method: {100=I, 101=Love, 102=Core, 103=Java}\r\n\r\nAfter using Clone method: {100=I, 101=Love, 102=Core, 103=Java}\r\n<\/pre>\n<p>Read All <a href=\"\/java\/treemap-methods.html\">TreeMap\u00a0Methods in JAVA with Example<\/a><\/p>\n<hr \/>\n<h4><strong>Important Points About TreeMap<\/strong><\/h4>\n<ul>\n<li>Tree Map only stores Unique values, that is duplicate values are not allowed.<\/li>\n<li>Tree Map maintains Ascending order corresponding to keys, which means it returns the key value pair in the increasing order, irrespective of order in which they are added.<\/li>\n<li>Tree Map does not allows null key but it can have null value in it<\/li>\n<li>Tree Map is very much similar to Hash Map, both are not thread safe and both are unsynchronized<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>TreeMap 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 Ascending order based on key<\/li>\n<li>It cannot have null key but can have 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>TreeMap is a type of Collection that stores values based on the corresponding key. This key value pair is often called as Entry. It implements the NavigableMap interface and extends AbstractMap class. It can \u00a0stores only unique elements, that is duplicate values are not allowed and it cannot store key as null but is can\u00a0 &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/treemap\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TreeMap 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-1032","page","type-page","status-publish","hentry"],"psp_head":"<title>TreeMap Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"TreeMap is a type of Collection that stores values based on the corresponding key. This key value pair is often called as Entry. It implements the NavigableMap interface and extends AbstractMap class.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/treemap\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1032","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=1032"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1032\/revisions"}],"predecessor-version":[{"id":1496,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1032\/revisions\/1496"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}