如何使用Java中的可比较接口对 LinkedHashSet 元素进行排序?
LinkedHashSet 是 HashSet 的有序版本,它在所有元素之间维护一个双向链表。当需要维护迭代顺序时,使用这个类。当遍历 HashSet 时,顺序是不可预测的,而 LinkedHashSet 允许我们按照元素插入的顺序遍历元素。
为了首先在Java中使用 Comparable 接口对 LinkedHashSet 元素进行排序,我们创建了一个实现 Comparable 接口的 Student 类。在这个类中,我们覆盖了 compareTo() 方法。
// Student class implements comparable interface
class Student implements Comparable {
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);
}
}
然后我们将集合传递给 TreeSet 构造函数以对元素进行排序。
// TreeSet to sort LinkedHashSet using comparable
TreeSet tree_set = new TreeSet<>(set);
示例 1
Java
// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
import java.util.*;
// Student class implements comparable interface
class Student implements Comparable {
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);
}
}
class GFG {
public static void main(String[] args)
{
// New LinkedHashSet
LinkedHashSet 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 ascending order : "
+ set);
// TreeSet to sort LinkedHashSet using comparable
TreeSet tree_set = new TreeSet<>(set);
// Print after sorting
System.out.println(
"After sort elements in ascending order : "
+ tree_set);
}
}
Java
// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
import java.util.*;
// Student class implements comparable interface
class Student implements Comparable {
Integer marks;
Student(Integer marks) { this.marks = marks; }
// override toString method
public String toString() { return (" " + this.marks); }
// Override compareTo method to sort LinkedHashSet in
// descending order
public int compareTo(Student stu)
{
return stu.marks.compareTo(this.marks);
}
}
class GFG {
public static void main(String[] args)
{
// New LinkedHashSet
LinkedHashSet 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 descending order : "
+ set);
// TreeSet to sort LinkedHashSet using comparable
TreeSet tree_set = new TreeSet<>(set);
// Print after sorting
System.out.println(
"After sort elements in descending order : "
+ tree_set);
}
}
输出
Before sort elements in ascending order : [ 500, 300, 400, 100, 200]
After sort elements in ascending order : [ 100, 200, 300, 400, 500]
示例 2
Java
// Java program demonstrate how to Sort LinkedHashSet using
// Comparable interface
import java.util.*;
// Student class implements comparable interface
class Student implements Comparable {
Integer marks;
Student(Integer marks) { this.marks = marks; }
// override toString method
public String toString() { return (" " + this.marks); }
// Override compareTo method to sort LinkedHashSet in
// descending order
public int compareTo(Student stu)
{
return stu.marks.compareTo(this.marks);
}
}
class GFG {
public static void main(String[] args)
{
// New LinkedHashSet
LinkedHashSet 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 descending order : "
+ set);
// TreeSet to sort LinkedHashSet using comparable
TreeSet tree_set = new TreeSet<>(set);
// Print after sorting
System.out.println(
"After sort elements in descending order : "
+ tree_set);
}
}
输出:
Before sort elements in descending order : [ 500, 300, 400, 100, 200]
After sort elements in descending order : [ 500, 400, 300, 200, 100]