📌  相关文章
📜  使用Java中的 Comparable 接口按键对 LinkedHashMap 进行排序

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

使用Java中的 Comparable 接口按键对 LinkedHashMap 进行排序

LinkedHashMap 就像 HashMap 一样,具有维护插入其中的元素顺序的附加功能。 HashMap 从来没有维护过 LinkedHashMap 提供的插入的轨迹和顺序,可以按照插入顺序访问元素。

为了首先使用Java中的可比较接口按键对 LinkedHashMap 进行排序,我们创建了一个实现可比较接口的类。在这个类中,我们覆盖了 compareTo() 方法。

// Student class implements comparable interface

class Student implements Comparable {
    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);
    }
}

然后我们将 LinkedHashMap 传递给 TreeMap 构造函数进行排序。

TreeMap tree_map = new TreeMap<>(map);

以下是该方法的完整实现:

示例 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 {
    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 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 tree_map
            = new TreeMap<>(map);
 
        // Print after sorting
        System.out.println(
            "After sort keys in ascending order : "
            + tree_map);
    }
}


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 {
    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 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 tree_map
            = new TreeMap<>(map);
 
        // Print after sorting
        System.out.println(
            "After sort keys in descending 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}

示例 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 {
    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 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 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}