Java Collections

TreeSet Class in Collection

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<pre class&equals;"highlight" name&equals;"code">public class TreeSet&lt&semi;E&gt&semi; &NewLine;extends AbstractSet&lt&semi;E&gt&semi; &NewLine;implements NavigableSet&lt&semi;E&gt&semi;&comma; Cloneable&comma; Serializable &NewLine;<&sol;pre>&NewLine;<p>A <i><b>NavigableSet <&sol;b><&sol;i>implementation based on a <i><b>TreeMap<&sol;b><&sol;i>&period; The elements are ordered using their natural ordering&comma; or by a <i><b>Comparator <&sol;b><&sol;i>provided at set creation time&comma; depending on which constructor is used&period;<&sol;p>&NewLine;<p> This implementation provides guaranteed <b><i>log&lpar;n&rpar;<&sol;i><&sol;b> time cost for the basic operations &lpar;<b>add&comma; remove and contains&rpar;<&sol;b>&period;<&sol;p>&NewLine;<p> Note that the ordering maintained by a set &lpar;whether or not an explicit comparator is provided&rpar; must be consistent with equals if it is to correctly implement the <i><b>Set <&sol;b><&sol;i>interface&period; This is so because the <i><b>Set <&sol;b><&sol;i>interface is defined in terms of the equals operation&comma; but a <i><b>TreeSet <&sol;b><&sol;i>instance performs all element comparisons using its <i><b>compareTo &lpar;or compare&rpar;<&sol;b><&sol;i> method&comma; so two elements that are deemed equal by this method are&comma; from the standpoint of the set&comma; equal&period; The behavior of a set is well-defined even if its ordering is inconsistent with equals&semi; it just fails to obey the general contract of the <i><b>Set <&sol;b><&sol;i>interface&period; <&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>Note that this implementation is not synchronized&period; <&sol;b>If multiple threads access a tree set concurrently&comma; and at least one of the threads modifies the set&comma; it must be synchronized externally&period; This is typically accomplished by synchronizing on some object that naturally encapsulates the set&period; If no such object exists&comma; the set should be &&num;8220&semi;wrapped&&num;8221&semi; using the <b><i>Collections&period;synchronizedSortedSet<&sol;i><&sol;b> method&period; This is best done at creation time&comma; to prevent accidental unsynchronized access to the set&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">SortedSet s &equals; Collections&period;synchronizedSortedSet&lpar;new TreeSet&lpar;&period;&period;&period;&rpar;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><i><b>TreeSet <&sol;b><&sol;i>provides an implementation of the <b><i>Set <&sol;i><&sol;b>interface that uses a tree for storage&period; Objects are stored in sorted&comma; ascending order&period; Access and retrieval times are quite fast&comma; which makes <i><b>TreeSet <&sol;b><&sol;i>an excellent choice when storing large amounts of sorted information that must be found quickly&period; <&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;treeset&period;jpg"&sol;><&sol;div>&NewLine;<p>&NewLine; The <i><b>TreeSet <&sol;b><&sol;i>class supports four <i><b>constructors<&sol;b><&sol;i>&period; The first form constructs an empty tree set that will be sorted in ascending order according to the natural order of its elements&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">TreeSet&lpar; &rpar; &NewLine;&sol;&sol;The second form builds a tree set that contains the elements of c&period; &NewLine;TreeSet&lpar;Collection c&rpar; &NewLine; &NewLine;&sol;&sol;The third form constructs an empty tree set that will be sorted according to the comparator specified by comp&period; &NewLine;TreeSet&lpar;Comparator comp&rpar; &NewLine; &NewLine;&sol;&sol;The fourth form builds a tree set that contains the elements of ss&colon; &NewLine;TreeSet&lpar;SortedSet ss&rpar; &NewLine;<&sol;pre>&NewLine;<p><b>Methods in <i>TreeSet<&sol;i>&colon;<&sol;b><&sol;p>&NewLine;<table class&equals;"src">&NewLine;<tbody>&NewLine;<tr>&NewLine;<th>SN<&sol;th>&NewLine;<th>Methods with Description<&sol;th>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>1<&sol;td>&NewLine;<td><i><b>void add&lpar;Object o&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Adds the specified element to this set if it is not already present&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>2<&sol;td>&NewLine;<td><i><b>boolean addAll&lpar;Collection c&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Adds all of the elements in the specified collection to this set&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>3<&sol;td>&NewLine;<td><i><b>void clear&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Removes all of the elements from this set&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>4<&sol;td>&NewLine;<td><i><b>Object clone&lpar;&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Returns a shallow copy of this TreeSet instance&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>5<&sol;td>&NewLine;<td><i><b>Comparator comparator&lpar;&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Returns the comparator used to order this sorted set&comma; or null if this tree set uses its elements natural ordering&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>6<&sol;td>&NewLine;<td><b><i>boolean contains&lpar;Object o&rpar;<&sol;i> <&sol;b><br &sol;>&NewLine;Returns true if this set contains the specified element&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>7<&sol;td>&NewLine;<td><i><b>Object first&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns the first &lpar;lowest&rpar; element currently in this sorted set&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>8<&sol;td>&NewLine;<td><i><b>SortedSet headSet&lpar;Object toElement&rpar;<&sol;b><&sol;i><br &sol;>&NewLine;Returns a view of the portion of this set whose elements are strictly less than toElement&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>9<&sol;td>&NewLine;<td><i><b>boolean isEmpty&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns true if this set contains no elements&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>10<&sol;td>&NewLine;<td><i><b>Iterator iterator&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns an iterator over the elements in this set&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>11<&sol;td>&NewLine;<td><i><b>Object last&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns the last &lpar;highest&rpar; element currently in this sorted set&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>12<&sol;td>&NewLine;<td><i><b>boolean remove&lpar;Object o&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Removes the specified element from this set if it is present&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>13<&sol;td>&NewLine;<td><i><b>int size&lpar;&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns the number of elements in this set &lpar;its cardinality&rpar;&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>14<&sol;td>&NewLine;<td><i><b>SortedSet subSet&lpar;Object fromElement&comma; Object toElement&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns a view of the portion of this set whose elements range from fromElement&comma; inclusive&comma; to toElement&comma; exclusive&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>15<&sol;td>&NewLine;<td><i><b>SortedSet tailSet&lpar;Object fromElement&rpar; <&sol;b><&sol;i><br &sol;>&NewLine;Returns a view of the portion of this set whose elements are greater than or equal to fromElement&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<p>&NewLine;<b>Example of <i>TreeSet <&sol;i>class&colon;<&sol;b><br &sol;>&NewLine;The following program illustrates several of the methods supported by this collection&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class TreeSetDemo &lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; Create a tree set &NewLine; TreeSet ts &equals; new TreeSet&lpar;&rpar;&semi; &NewLine; &sol;&sol; Add elements to the tree set &NewLine; ts&period;add&lpar;"C"&rpar;&semi; &NewLine; ts&period;add&lpar;"A"&rpar;&semi; &NewLine; ts&period;add&lpar;"B"&rpar;&semi; &NewLine; ts&period;add&lpar;"E"&rpar;&semi; &NewLine; ts&period;add&lpar;"F"&rpar;&semi; &NewLine; ts&period;add&lpar;"D"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;ts&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;treeset&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>Example of <i>TreeSet <&sol;i>adding duplicate value&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;util&period;&ast;&semi; &NewLine; &NewLine;public class TreeSetDemo &lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; &sol;&sol; Create a tree set &NewLine; TreeSet ts &equals; new TreeSet&lpar;&rpar;&semi; &NewLine; &sol;&sol; Add elements to the tree set &NewLine; ts&period;add&lpar;"Dinesh"&rpar;&semi; &NewLine; ts&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; ts&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; ts&period;add&lpar;"Anamika"&rpar;&semi; &NewLine; ts&period;add&lpar;"Sweety"&rpar;&semi; &NewLine; ts&period;add&lpar;"Raj"&rpar;&semi; &NewLine; System&period;out&period;println&lpar;ts&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;treeset2&period;png"&sol;><&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<b>Java Collections Tutorial<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;arraylist-class-in-java-collection&sol;">ArrayList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedlist-class-in-collection&sol;">LinkedList class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;listiterator-interface-in-collection&sol;">ListIterator interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashset-class-in-collection&sol;">HashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">LinkedHashSet class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Difference between TreeSet &amp&semi; HashSet<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;map-interface-in-collection&sol;">Map interface<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashmap-class-in-collection-framework&sol;">HashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-does-java-hashmap-work-internally&sol;">How does work HashMap&quest;<&sol;a> <&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-treemap-vs-hashmap&sol;">Difference between TreeMap vs HashMap<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashmap-class-in-collection&sol;">LinkedHashMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;treemap-class-in-collection-framework&sol;">TreeMap class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashtable-class-in-collection-framework&sol;">Hashtable class<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashmap-and&sol;">Difference between HashMap and HashTable in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;sorting-in-collection-framework&sol;">Sorting<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-comparable-and-comparator&sol;">Comparable interface<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;linkedhashset-class-in-collection&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-hashset-and-treeset&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '462'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago