TreeSet is one of the implementations of the SortedSet interface in Java that uses a Red-black tree for storage. By default, the elements are stored in ascending order. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used.
Geeks for Geeks Welcome to
The SortedSet interface is available in java.util package which extends the Set interface. This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this interface to be stored in a sorted manner.
[Bobby, Devi2, Gnanesh, Ojaswi, Rohith, Sravan] Iterating over set: Bobby Devi2 Gnanesh Rohith Sravan
Differences between TreeSet and SortedSet TreeSet SortedSet Syntax: TreeSet<String> treeset = new TreeSet<String>(); Syntax: It can not be instantiated as it is a Interface
TreeSet is a concrete class. SortedSet is an interface. TreeSet allows a heterogeneous object. SortedSet allows a heterogeneous object. TreeSet maintains an object in sorted order. SortedSet maintains an object in sorted order.
TreeSet<String> treeset = new TreeSet<String>();
It can not be instantiated as it is a Interface
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.