How to Sort TreeSet Elements using Comparable Interface in Java? Last Updated : 01 Oct, 2021 Comments Improve Suggest changes Like Article Like Report TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a Set using their natural ordering whether an explicit comparator is provided. To sort TreeSet elements using Comparable interface in java first, we create a class Student that implements the Comparable interface. In this class we override the compareTo() method. Pseudo Code: // Student class implements comparable interface class Student implements Comparable<Student> { Integer marks; Student(Integer marks) { this.marks = marks; } // override toString method public String toString() { return (" " + this.marks); } // Override compareTo method to sort LinkedHashSet in ascending order public int compareTo(Student stu) { return this.marks.compareTo(stu.marks); } } Below is the implementation of the above approach: Example 1: Java // Java program to demonstrate how to Sort TreeSet using // Comparable interface in ascending order import java.util.*; // Student class implements comparable interface class Student implements Comparable<Student> { Integer marks; Student(Integer marks) { this.marks = marks; } // override toString method public String toString() { return (" " + this.marks); } // Override compareTo method to sort TreeSet in // ascending order public int compareTo(Student stu) { return this.marks.compareTo(stu.marks); } } class GFG { public static void main(String[] args) { // New TreeSet TreeSet<Student> set = new TreeSet<>(); // Adding elements to the set set.add(new Student(500)); set.add(new Student(300)); set.add(new Student(400)); set.add(new Student(100)); set.add(new Student(200)); // Print TreeSet sorted in ascending order System.out.println("Sort elements in ascending order : " + set); } } OutputSort elements in ascending order : [ 100, 200, 300, 400, 500] Example 2: Java // Java program demonstrate how to Sort TreeSet using // Comparable interface in descending order import java.util.*; // Student class implements comparable interface class Student implements Comparable<Student> { Integer marks; Student(Integer marks) { this.marks = marks; } // override toString method public String toString() { return (" " + this.marks); } // Override compareTo method to sort TreeSet in // descending order public int compareTo(Student stu) { return stu.marks.compareTo(this.marks); } } class GFG { public static void main(String[] args) { // New TreeSet TreeSet<Student> set = new TreeSet<>(); // Adding elements to the set set.add(new Student(500)); set.add(new Student(300)); set.add(new Student(400)); set.add(new Student(100)); set.add(new Student(200)); // Print TreeSet sorted in descending order System.out.println("Sort elements in descending order : " + set); } } OutputSort elements in descending order : [ 500, 400, 300, 200, 100] Comment More infoAdvertise with us Next Article How to Sort TreeSet Elements using Comparable Interface in Java? K KapilChhipa Follow Improve Article Tags : Java Java Programs Java-Collections java-treeset Java-Comparable +1 More Practice Tags : JavaJava-Collections Similar Reads How to Sort Vector Elements using Comparable Interface in Java? Vector is a child interface of collection. If we want to represent a group of the individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for vector. It is a resizable or growable array. It implements a Serializable, Cloneable, and R 5 min read How to Sort HashSet Elements using Comparable Interface in Java? The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means when we iterate the HashSet, there is no guarantee that we get elements in which order they were inserted. To sort HashSe 3 min read How to Sort LinkedHashSet Elements using Comparable Interface in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 3 min read How to Create TreeMap Objects using Comparable Interface in Java? In Java, the TreeMap class is an implementation of the SortedMap interface that stores key-value pairs in a sorted order based on the natural ordering of the keys. By default, the keys are sorted in ascending order. If you want to sort the keys based on a custom ordering criteria, you can use the Co 6 min read How to Iterate Over the Elements in a TreeSet in Natural Order in Java? In Java, to iterate over the elements of a TreeSet in their Natural Order, one must either use a custom comparator provided or traverse the elements in ascending order based on their natural ordering. A TreeSet in Java keeps up with its components in arranged requests. In this article, we will learn 2 min read Like