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 HashSet 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);
}
}
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);
Below is the full implementation of the above approach:
Example 1:
Java
import java.util.*;
class Student implements Comparable<Student> {
Integer marks;
Student(Integer marks) { this .marks = marks; }
public String toString() { return ( " " + this .marks); }
public int compareTo(Student stu)
{
return this .marks.compareTo(stu.marks);
}
}
class GFG {
public static void main(String[] args)
{
HashSet<Student> set = new HashSet<>();
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 ));
System.out.println( "Before sort elements in ascending order : "
+ set);
TreeSet<Student> tree_set = new TreeSet<>(set);
System.out.println( "After sort elements in ascending order : "
+ tree_set);
}
}
|
Output
Before sort elements in ascending order : [ 300, 400, 500, 200, 100]
After sort elements in ascending order : [ 100, 200, 300, 400, 500]
Example 2:
Java
import java.util.*;
class Student implements Comparable<Student> {
Integer marks;
Student(Integer marks) { this .marks = marks; }
public String toString() { return ( " " + this .marks); }
public int compareTo(Student stu)
{
return stu.marks.compareTo( this .marks);
}
}
class GFG {
public static void main(String[] args)
{
HashSet<Student> set = new HashSet<>();
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 ));
System.out.println( "Before sort elements in descending order : "
+ set);
TreeSet<Student> tree_set = new TreeSet<>(set);
System.out.println( "After sort elements in descending order : "
+ tree_set);
}
}
|
Output
Before sort elements in descending order : [ 300, 400, 500, 200, 100]
After sort elements in descending order : [ 500, 400, 300, 200, 100]
Last Updated :
01 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...