A HashSet stores only unique elements but does not maintain any insertion or sorting order. To sort its elements, implement the Comparable interface in the object class and pass the HashSet to a TreeSet, which orders the elements using the compareTo() method.
- HashSet stores only unique elements but does not maintain any order.
- Comparable defines the natural ordering of objects through the compareTo() method.
- TreeSet automatically sorts elements using their natural ordering.
import java.util.*;
class Student implements Comparable<Student> {
int marks;
Student(int marks) {
this.marks = marks;
}
@Override
public int compareTo(Student s) {
return this.marks - s.marks;
}
@Override
public String toString() {
return String.valueOf(marks);
}
}
public class Main {
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(100));
TreeSet<Student> sortedSet = new TreeSet<>(set);
System.out.println(sortedSet);
}
}
Output
[100, 300, 500]
How it Works
- Create a class that implements the Comparable interface.
- Override the compareTo() method to define the sorting logic.
- Store objects inside a HashSet.
- Pass the HashSet to the TreeSet constructor.
- The TreeSet automatically stores the elements in sorted order.
Syntax
Comparable Interface
class ClassName implements Comparable<ClassName> {
@Override
public int compareTo(ClassName obj) {
// comparison logic
}
}
Parameters:
- obj: The object to compare with the current object.
Return Value:
compareTo(): Returns an integer value.
- Returns 0 if both objects are equal.
- Returns a negative value if the current object is smaller.
- Returns a positive value if the current object is greater.
Example: Sort HashSet in Ascending Order
import java.util.*;
class Student implements Comparable<Student> {
Integer marks;
Student(Integer marks) {
this.marks = marks;
}
@Override
public String toString() {
return " " + marks;
}
@Override
public int compareTo(Student stu) {
return this.marks.compareTo(stu.marks);
}
}
public 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 Sorting: " + set);
TreeSet<Student> sortedSet = new TreeSet<>(set);
System.out.println("After Sorting: " + sortedSet);
}
}
Output
Before Sorting: [ 300, 500, 100, 400, 200] After Sorting: [ 100, 200, 300, 400, 500]
Example: Sort HashSet in Descending Order
import java.util.*;
class Student implements Comparable<Student> {
Integer marks;
Student(Integer marks) {
this.marks = marks;
}
@Override
public String toString() {
return " " + marks;
}
@Override
public int compareTo(Student stu) {
return stu.marks.compareTo(this.marks);
}
}
public 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 Sorting: " + set);
TreeSet<Student> sortedSet = new TreeSet<>(set);
System.out.println("After Sorting: " + sortedSet);
}
}
Output
Before Sorting: [ 300, 500, 100, 400, 200] After Sorting: [ 500, 400, 300, 200, 100]