Open In App

How to Sort LinkedHashSet Elements using Comparable Interface in Java?

Last Updated : 04 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ascending 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
importjava.util.*;
 
// Student class implements comparable interface
classStudent implementsComparable<Student> {
    Integer marks;
 
    Student(Integer marks) { this.marks = marks; }
 
    // override toString method
    publicString toString() { return(" "+ this.marks); }
 
    // Override compareTo method to sort LinkedHashSet in
    // ascending order
    publicintcompareTo(Student stu)
    {
        returnthis.marks.compareTo(stu.marks);
    }
}
 
classGFG {
    publicstaticvoidmain(String[] args)
    {
 
        // New LinkedHashSet
        LinkedHashSet<Student> set = newLinkedHashSet<>();
 
        // Adding elements to the set
        set.add(newStudent(500));
        set.add(newStudent(300));
        set.add(newStudent(400));
        set.add(newStudent(100));
        set.add(newStudent(200));
 
        // Print Before sort
        System.out.println(
            "Before sort elements in ascending order : "
            + set);
 
        // TreeSet to sort LinkedHashSet using comparable
        TreeSet<Student> tree_set = newTreeSet<>(set);
 
        // Print after sorting
        System.out.println(
            "After sort elements in ascending order : "
            + tree_set);
    }
}

Output

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

Example 2

Java




// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
importjava.util.*;
 
// Student class implements comparable interface
classStudent implementsComparable<Student> {
    Integer marks;
 
    Student(Integer marks) { this.marks = marks; }
 
    // override toString method
    publicString toString() { return(" "+ this.marks); }
 
    // Override compareTo method to sort LinkedHashSet in
    // descending order
    publicintcompareTo(Student stu)
    {
        returnstu.marks.compareTo(this.marks);
    }
}
 
classGFG {
    publicstaticvoidmain(String[] args)
    {
 
        // New LinkedHashSet
        LinkedHashSet<Student> set = newLinkedHashSet<>();
 
        // Adding elements to the set
        set.add(newStudent(500));
        set.add(newStudent(300));
        set.add(newStudent(400));
        set.add(newStudent(100));
        set.add(newStudent(200));
 
        // Print Before sort
        System.out.println(
            "Before sort elements in descending order : "
            + set);
 
        // TreeSet to sort LinkedHashSet using comparable
        TreeSet<Student> tree_set = newTreeSet<>(set);
 
        // Print after sorting
        System.out.println(
            "After sort elements in descending order : "
            + tree_set);
    }
}

Output:

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


Next Article

Similar Reads

close