Skip to content
Related Articles

Related Articles

How to Sort TreeSet Elements using Comparable Interface in Java?
  • Last Updated : 19 Jan, 2021

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 accending 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
    // accending 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 accending order
        System.out.println("Sort elements in ascending order : " + set);
        
    }
}
Output
Sort 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
    // decending 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 decending order
        System.out.println("Sort elements in decending order : " + set);
        
    }
}
Output
Sort elements in decending order : [ 500,  400,  300,  200,  100]

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :