📌  相关文章
📜  如何使用Java中的可比较接口对 TreeSet 元素进行排序?

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

如何使用Java中的可比较接口对 TreeSet 元素进行排序?

TreeSet是Java中 SortedSet 接口的实现,它使用 Tree 进行存储。无论是否提供显式比较器,元素的顺序都由 Set 使用它们的自然顺序来维护。

为了首先在Java中使用Comparable接口对 TreeSet 元素进行排序,我们创建了一个实现 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);
    }
}

下面是上述方法的实现:

示例 1:

Java
// Java program to demonstrate how to Sort TreeSet using
// Comparable interface in ascending order
 
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 TreeSet in
    // ascending order
    public int compareTo(Student stu)
    {
        return this.marks.compareTo(stu.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New TreeSet
        TreeSet set = new TreeSet<>();
 
        // 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 TreeSet sorted in ascending order
        System.out.println("Sort elements in ascending order : " + set);
       
    }
}


Java
// Java program demonstrate how to Sort TreeSet using
// Comparable interface in descending order
 
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 TreeSet in
    // descending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New TreeSet
        TreeSet set = new TreeSet<>();
 
        // 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 TreeSet sorted in descending order
        System.out.println("Sort elements in descending order : " + set);
       
    }
}


输出
Sort elements in ascending order : [ 100,  200,  300,  400,  500]

示例 2:

Java

// Java program demonstrate how to Sort TreeSet using
// Comparable interface in descending order
 
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 TreeSet in
    // descending order
    public int compareTo(Student stu)
    {
        return stu.marks.compareTo(this.marks);
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // New TreeSet
        TreeSet set = new TreeSet<>();
 
        // 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 TreeSet sorted in descending order
        System.out.println("Sort elements in descending order : " + set);
       
    }
}
输出
Sort elements in descending order : [ 500,  400,  300,  200,  100]