Skip to content
Related Articles

Related Articles

Improve Article

How to Sort LinkedHashSet Elements using Comparable Interface in Java?

  • Last Updated : 04 Jan, 2021

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 in the order in which they were inserted.

To sort LinkedHashSet 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.

// 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);
    }
}

And then we pass the set to the TreeSet constructor to sort the elements.

// TreeSet to sort LinkedHashSet using comparable
TreeSet<Student> tree_set = new TreeSet<>(set);

Example 1

Java




// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
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 LinkedHashSet in
    // accending order
    public int compareTo(Student stu)
    {
        return this.marks.compareTo(stu.marks);
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        // New LinkedHashSet
        LinkedHashSet<Student> set = new LinkedHashSet<>();
  
        // 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 Before sort
        System.out.println(
            "Before sort elements in accending order : "
            + set);
  
        // TreeSet to sort LinkedHashSet using comparable
        TreeSet<Student> tree_set = new TreeSet<>(set);
  
        // Print after sorting
        System.out.println(
            "After sort elements in accending order : "
            + tree_set);
    }
}
Output



Before sort elements in accending order : [ 500,  300,  400,  100,  200]
After sort elements in accending order : [ 100,  200,  300,  400,  500]

Example 2

Java




// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
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 LinkedHashSet in
    // decending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        // New LinkedHashSet
        LinkedHashSet<Student> set = new LinkedHashSet<>();
  
        // 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 Before sort
        System.out.println(
            "Before sort elements in decending order : "
            + set);
  
        // TreeSet to sort LinkedHashSet using comparable
        TreeSet<Student> tree_set = new TreeSet<>(set);
  
        // Print after sorting
        System.out.println(
            "After sort elements in decending order : "
            + tree_set);
    }
}
Output
Before sort elements in decending order : [ 500,  300,  400,  100,  200]
After 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 :