📌  相关文章
📜  如何在Java中使用 Comparable 接口对 HashSet 元素进行排序?

📅  最后修改于: 2022-05-13 01:55:10.478000             🧑  作者: Mango

如何在Java中使用 Comparable 接口对 HashSet 元素进行排序?

HashSet类实现了 Set 接口,由一个哈希表支持,它实际上是一个 HashMap 实例。不能保证集合的迭代顺序,这意味着当我们迭代 HashSet 时,不能保证我们得到元素的插入顺序。

为了首先在Java中使用Comparable接口对 HashSet 元素进行排序,我们创建了一个实现 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 to demonstrate how to Sort HashSet 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 HashSet in
    // ascending order
    public int compareTo(Student stu)
    {
        return this.marks.compareTo(stu.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New HashSet
        HashSet set = new HashSet<>();
 
        // 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 HashSet using comparable
        // interface
        TreeSet tree_set = new TreeSet<>(set);
 
        // Print after sorting
        System.out.println("After sort elements in ascending order : "
            + tree_set);
    }
}


Java
// Java program to demonstrate how to Sort HashSet 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 HashSet in
    // decending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New HashSet
        HashSet set = new HashSet<>();
 
        // 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 HashSet using comparable
        // interface
        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 : [ 300,  400,  500,  200,  100]
After sort elements in ascending order : [ 100,  200,  300,  400,  500]

示例 2:

Java

// Java program to demonstrate how to Sort HashSet 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 HashSet in
    // decending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New HashSet
        HashSet set = new HashSet<>();
 
        // 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 HashSet using comparable
        // interface
        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 : [ 300,  400,  500,  200,  100]
After sort elements in descending order : [ 500,  400,  300,  200,  100]