{"id":715,"date":"2016-03-01T06:07:47","date_gmt":"2016-03-01T06:07:47","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=715"},"modified":"2018-06-05T06:41:44","modified_gmt":"2018-06-05T06:41:44","slug":"treeset","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/treeset","title":{"rendered":"TreeSet Tutorial In Java With Example"},"content":{"rendered":"<p>TreeSet is a type of Collection, that implements Set Interface. It is mostly similar to HashSet class that is it do not allow duplicate elements to be stored.<\/p>\n<p>In TreeSet all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary values.\u00a0Apart from adding this functionality of maintaining natural ordering, Tree Set do not allow null values.<\/p>\n<p>TreeSet is best choice for storing large amount of data, as its retrieval and access time is very fast, which makes data to be found in no time.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Hash set does not maintains any order, that is when we retrieve values from it we do not get that values in the same order we have entered in it.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Hierarchy of TreeSet class:<\/strong><\/span><\/h4>\n<p>TreeSet implements NavigableSet Interface which further extends SortedSet, Set, Collection, and Iterable Interfaces, as shown in figure 1.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-957\" src=\"\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-TreeSet-class-2.png\" alt=\"Hierarchy of TreeSet class\" width=\"215\" height=\"503\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-TreeSet-class-2.png 266w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-TreeSet-class-2-128x300.png 128w\" sizes=\"auto, (max-width: 215px) 100vw, 215px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>\u00a0<span style=\"color: #008000;\">Example of TreeSet class:\u00a0<\/span><\/strong><\/h4>\n<p>Let us discuss TreeSet with the help of program. Following program has been divided into 3 Steps that we will discuss one by one:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreSetMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  \/\/Step 1\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject1 = new TreeSet&lt;Integer&gt;();\r\n\r\n  treeSetObject1.add(3); \r\n  \r\n  \/\/Step 2\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(4);\r\n\r\n  treeSetObject1.add(1);\r\n\r\n  treeSetObject1.add(2);\r\n\r\n  treeSetObject1.add(8);\r\n\r\n  treeSetObject1.add(9);\r\n\r\n  treeSetObject1.add(10);\r\n\r\n  treeSetObject1.add(9);\r\n\r\n  treeSetObject1.add(12);\r\n\r\n   \/\/Step 3\r\n\r\n  System.out.println(\"Values in tree Set Object 1 :\" +treeSetObject1);    \r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in tree Set Object 1:[1, 2, 3, 4, 5, 8, 9, 10, 12]<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we have created an object of TreeSet collection, we have defined element to be stored of Integer type.<\/li>\n<\/ul>\n<ul>\n<li>In Step 2, we have used add method to add elements of Integer type in the TreeSet object that we have created in Step 1.<\/li>\n<\/ul>\n<ul>\n<li>In Step 3, we have printed all the values of TreeSet object.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>TreeSet Methods:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. boolean add(Object o):<\/strong><\/span><\/p>\n<p>This methods adds element in the object, which will automatically stores all the elements in increasing order, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreSetMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject1 = new TreeSet&lt;Integer&gt;();\r\n\r\n  TreeSet &lt;String&gt;treeSetObject2 = new TreeSet&lt;String&gt;();\r\n\r\n  treeSetObject1.add(3);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(1);\r\n\r\n  treeSetObject1.add(2);\r\n\r\n  treeSetObject2.add(\"B\");\r\n\r\n  treeSetObject2.add(\"C\");\r\n\r\n  treeSetObject2.add(\"A\");\r\n\r\n  treeSetObject2.add(\"D\");\r\n\r\n  treeSetObject2.add(\"A\");\r\n\r\n  System.out.println(\"Values in TreeSet object 1: \" +treeSetObject1);\r\n\r\n  System.out.println(\"Values in TreeSet object 2: \u00a0\" +treeSetObject2);\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in TreeSet object 1: [1, 2, 3, 5]\r\n\r\nValues in TreeSet object 2: [A, B, C, D]<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. boolean addAll(Collection c):<\/strong><\/span><\/p>\n<p>This method adds all the elements of one object to another , as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreSetMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject1 = new TreeSet&lt;Integer&gt;();\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject2 = new TreeSet&lt;Integer&gt;();\r\n\r\n  treeSetObject1.add(3);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(1);\r\n\r\n  treeSetObject1.add(2);\r\n\r\n  treeSetObject2.add(9);\r\n\r\n  treeSetObject2.add(11);\r\n\r\n  treeSetObject2.add(1);\r\n\r\n  treeSetObject2.add(8);\r\n\r\n  treeSetObject2.add(7);\r\n\r\n  treeSetObject1.addAll(treeSetObject2);\r\n\r\n  System.out.println(\"Values in TreeSet object 1: \" +treeSetObject1);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in TreeSet object 1: [1, 2, 3, 5, 7, 8, 9, 11]<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. Object ceiling(Object o):<\/strong><\/span><\/p>\n<p>This method retrieves the least element which is greater than or equal to the given element in the argument list, or null if there is no such element in the set as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreSetMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject1 = new TreeSet&lt;Integer&gt;();\r\n\r\n  treeSetObject1.add(3);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(1);\r\n\r\nSystem.out.println(\"Ceiling value of '5' is:\" +treeSetObject1.ceiling(5));\r\n\r\nSystem.out.println(\"Ceiling value of '2' is:\" +treeSetObject1.ceiling(2));\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Ceiling value of '5' is:5\r\n\r\nCeiling value of '2' is:3<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. void clear():<\/strong><\/span><\/p>\n<p>This method removes all of the elements from this object, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class TreSetMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  TreeSet &lt;Integer&gt;treeSetObject1 = new TreeSet&lt;Integer&gt;();\r\n\r\n  treeSetObject1.add(3);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(5);\r\n\r\n  treeSetObject1.add(1);\r\n\r\n  treeSetObject1.add(2);\r\n\r\n  System.out.println(\"Values in TreeSet object 1:\" +treeSetObject1);\r\n\r\n  treeSetObject1.clear();\r\n\r\n  System.out.println(\"Values in TreeSet object 1:\" +treeSetObject1);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in TreeSet object 1:[1, 2, 3, 5]\r\n\r\nValues in TreeSet object 1:[]\r\n<\/pre>\n<p>Read All <a href=\"\/java\/treeset-methods-java-examples.html\">TreeSet\u00a0Methods In JAVA With Example<\/a><\/p>\n<hr \/>\n<h4><strong>\u00a0<span style=\"color: #008000;\">TreeSet Important Points To Remember:<\/span><\/strong><\/h4>\n<ul>\n<li>TreeSet only stores unique values, that is duplicate values are not allowed.<\/li>\n<li>TreeSet stores values in natural ordering, which means it returns the elements in the increasing order.<\/li>\n<li>TreeSet does not allows null values.<\/li>\n<li>As its Access and Retrieval time is fast as compared to HashSet, it is best choice for storing large amount of data.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>\u00a0<span style=\"color: #008000;\">Quick Revision Points About TreeSet:<\/span><\/strong><\/h4>\n<ul>\n<li>Stores Values in ascending order.<\/li>\n<li>No duplicate values are allowed.<\/li>\n<li>No null value is allowed.<\/li>\n<li>Best for large amount of data.<\/li>\n<li>Faster Access and Retrieval time as compared to HashSet.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>TreeSet is a type of Collection, that implements Set Interface. It is mostly similar to HashSet class that is it do not allow duplicate elements to be stored. In TreeSet all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/treeset\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TreeSet 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-715","page","type-page","status-publish","hentry"],"psp_head":"<title>TreeSet Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"TreeSet is a type of Collection, in which all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary values.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/treeset\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/715","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=715"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/715\/revisions"}],"predecessor-version":[{"id":1498,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/715\/revisions\/1498"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}