LinkedHashSet es una versión ordenada de HashSet que mantiene una lista doblemente vinculada en todos los elementos. Cuando se necesita mantener el orden de iteración, se utiliza esta clase. Al iterar a través de un HashSet, el orden es impredecible, mientras que un LinkedHashSet nos permite iterar a través de los elementos en el orden en que se insertaron.
Para ordenar los elementos LinkedHashSet usando la interfaz Comparable en Java primero, creamos una clase Student que implementa la interfaz Comparable. En esta clase, anulamos el método compareTo() .
// 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); } }
Y luego pasamos el conjunto al constructor TreeSet para ordenar los elementos.
// TreeSet to sort LinkedHashSet using comparable TreeSet<Student> tree_set = new TreeSet<>(set);
Ejemplo 1
Java
// Java program demonstrate how to Sort LinkedHashSet using // Comparable interface 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 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<Student> 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<Student> tree_set = new TreeSet<>(set); // Print after sorting System.out.println( "After sort elements in ascending 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]
Ejemplo 2
Java
// Java program demonstrate how to Sort LinkedHashSet using // Comparable interface 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 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<Student> 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<Student> tree_set = new TreeSet<>(set); // Print after sorting System.out.println( "After sort elements in descending order : " + tree_set); } }
Producción:
Before sort elements in descending order : [ 500, 300, 400, 100, 200] After 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