TreeSet es una implementación de la interfaz SortedSet en Java que utiliza un árbol para el almacenamiento. El orden de los elementos se mantiene mediante un Conjunto utilizando su orden natural si se proporciona un comparador explícito.
Para ordenar los elementos de TreeSet usando la interfaz Comparable en Java primero, creamos una clase Student que implementa la interfaz Comparable. En esta clase anulamos el método compareTo() .
Pseudocódigo:
// 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); } }
A continuación se muestra la implementación del enfoque anterior:
Ejemplo 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<Student> { 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<Student> 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); } }
Producción
Sort elements in ascending order : [ 100, 200, 300, 400, 500]
Ejemplo 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<Student> { 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<Student> 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); } }
Producción
Sort elements in descending order : [ 500, 400, 300, 200, 100]
Publicación traducida automáticamente
Artículo escrito por KapilChhipa y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA