LinkedHashMap es como HashMap con una característica adicional de mantener un orden de elementos insertados en él. HashMap nunca mantuvo la pista y el orden de inserción que proporciona LinkedHashMap donde se puede acceder a los elementos en su orden de inserción.
Para ordenar LinkedHashMap por claves usando la interfaz comparable en Java primero, creamos una clase que implementa la interfaz comparable. En esta clase, anulamos el método compareTo() .
// Student class implements comparable interface class Student implements Comparable<Student> { String name; Student(String name) { this.name = name; } // Override toString method public String toString() { return this.name; } // Override compareTo method public int compareTo(Student stu) { return this.name.compareTo(stu.name); } }
Y luego pasamos el LinkedHashMap al constructor TreeMap para ordenar.
TreeMap<Student, Integer> tree_map = new TreeMap<>(map);
A continuación se muestra la implementación completa del enfoque:
Ejemplo 1
Java
// Java program demonstrate how to Sort LinkedHashMap by // keys using Comparable interface import java.util.*; // Student class implements comparable interface class Student implements Comparable<Student> { String name; Student(String name) { this.name = name; } // override toString method public String toString() { return this.name; } // Override compareTo method to sort LinkedHashMap keys // in ascending order public int compareTo(Student stu) { return this.name.compareTo(stu.name); } } class GFG { public static void main(String[] args) { // New LinkedHashMap LinkedHashMap<Student, Integer> map = new LinkedHashMap<>(); // Adding elements to the map map.put(new Student("Bina"), 200); map.put(new Student("Akshay"), 400); map.put(new Student("Chintu"), 500); // Print Before sort System.out.println( "Before sort keys in ascending order : " + map); // TreeMap to sort LinkedHashMap using comparable TreeMap<Student, Integer> tree_map = new TreeMap<>(map); // Print after sorting System.out.println( "After sort keys in ascending order : " + tree_map); } }
Before sort keys in ascending order : {Bina=200, Akshay=400, Chintu=500} After sort keys in ascending order : {Akshay=400, Bina=200, Chintu=500}
Ejemplo 2
Java
// Java program demonstrate how to Sort LinkedHashMap by // keys using Comparable interface import java.util.*; // Student class implements comparable interface class Student implements Comparable<Student> { String name; Student(String name) { this.name = name; } // override toString method public String toString() { return this.name; } // Override compareTo method to sort LinkedHashMap keys // in descending order public int compareTo(Student stu) { return stu.name.compareTo(this.name); } } class GFG { public static void main(String[] args) { // New LinkedHashMap LinkedHashMap<Student, Integer> map = new LinkedHashMap<>(); // Adding elements to the map map.put(new Student("Bina"), 200); map.put(new Student("Akshay"), 400); map.put(new Student("Chintu"), 500); // Print Before sort System.out.println( "Before sort keys in descending order : " + map); // TreeMap to sort LinkedHashMap using comparable TreeMap<Student, Integer> tree_map = new TreeMap<>(map); // Print after sorting System.out.println( "After sort keys in descending order : " + tree_map); } }
Before sort keys in descending order : {Bina=200, Akshay=400, Chintu=500} After sort keys in descending order : {Chintu=500, Bina=200, Akshay=400}
Publicación traducida automáticamente
Artículo escrito por KapilChhipa y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA