Difference Between TreeSet and SortedSet in Java - GeeksforGeeks

01 Nov, 2021
TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For example, Comparator provided at set creation time, depending on which constructor is used.
It also implements NavigableSet interface.
NavigableSet extends SortedSet and Set interfaces.
Example
Java
// Java Program to Illustrate TreeSet // Importing required classesimport java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String args[]) {         // Creating an empty TreeSet of string type elements        TreeSet<String> al = new TreeSet<String>();         // Adding elements        // using add() method        al.add("Welcome");        al.add("to");        al.add("Geeks for Geeks");         // Traversing elements via help of iterators        Iterator<String> itr = al.iterator();         // Holds true until there is element remaining in object        while (itr.hasNext()) {             // Moving onto next element with help of next() method            System.out.println(itr.next());        }    }}
Geeks for Geeks
Welcome
to
Sorted Set
The SortedSet is a sub-interface, which is available in java.util.package which extends the Set interface. This interface contains the methods inherited from the Set interface. For example, headSet, tailSet, subSet, Comparator, first, last, and many more.
Example
Java
// Java program to Illustrate SortedSet // Importing utility classesimport java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creating an instance of SortedSet        // String type        SortedSet<String> ts = new TreeSet<String>();         // Adding elements into the TreeSet        // using add()        ts.add("Sravan");        ts.add("Ojaswi");        ts.add("Bobby");        ts.add("Rohith");        ts.add("Gnanesh");        ts.add("Devi2");         // Adding the duplicate element        // again simply using add() method        ts.add("Sravan");         // Print and display TreeSet        System.out.println(ts);         // Removing items from TreeSet        // using remove() method        ts.remove("Ojaswi");         // Display message        System.out.println("Iterating over set:");         // Iterating over TreeSet items        Iterator<String> i = ts.iterator();         // Condition holds true till there is single element        // remaining in the object        while (i.hasNext())             // Printing elements            System.out.println(i.next());    }}
[Bobby, Devi2, Gnanesh, Ojaswi, Rohith, Sravan]
Iterating over set:
Bobby
Devi2
Gnanesh
Rohith
Sravan
Now after having adequate understanding and internal working of both TreeSet and SortedSet, now let us see the differences between TreeSet and SortedSet which is as depicted from the table provided below as follows:
TreeSet
SortedSet
Syntax:
TreeSet<Datatype> treeset = new TreeSet<>();
Syntax:
It can not be instantiated as it is a sub-Interface.
Conclusion: Basically in simple words let’s assume this way, TreeSet is a class of NavigableSet which contains all of the methods for better traversing and searching for values. SortedSet is a sub-set of NavigableSet in terms of methods compare to TreeSet(NavigableSet)
harish garg
shivesh01
Java-SortedSet
java-treeset
Picked
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples