java 中对hashmap进行排序

复制代码
public class HashMapSort {
    public static void main(String[] args) {
      HashMap<Integer, Student> hashMap = new HashMap<Integer,Student>();
      hashMap.put(100, new Student("姚明",32));
      hashMap.put(2, new Student("TFboys",13));
      hashMap.put(30, new Student("刘翔",28));
      
      System.out.println("排序前:");
      System.out.println(""+hashMap.toString());
      System.out.println("排序后:");
      HashMap<Integer, Student> result  = sort(hashMap);
      System.out.println(""+ result);
    }

    private static HashMap<Integer, Student> sort(
            HashMap<Integer, Student> hashMap) {
        //第一步:LinkedHashMap采用的是链表结构,可以实现排序,并且是hashmap的子类
        //第二步:排序可以采用集合框架中的Collections.sort(list集合,比较器);
        //该方法可以自定义比较器
        //第三步:将参数hashmap转化成list集合,这一步是最关键的,hashmap有个成员函数enrtyset,可以将hashmap转化成set集合
        //set集合和list集合都属于collection的子类,list集合的构造函数可以是collection的子类,我们可以利用set集合构建
        //list集合达到我们将hashmap转化成list集合的目的
        //第五步:将排序之后的list集合存储在LinkedHashMap中
        
        LinkedHashMap<Integer, Student> linkedHashMap = new LinkedHashMap<Integer,Student>();
        Set<Entry<Integer, Student>> entrySet = hashMap.entrySet();
        ArrayList<Entry<Integer, Student>> arrayList = new ArrayList<>(entrySet);
        Collections.sort(arrayList,new Comparator<Entry<Integer, Student>>() {
            @Override
            public int compare(Entry<Integer, Student> obj1,
                    Entry<Integer, Student> obj2) {
                // TODO Auto-generated method stub
                return obj1.getValue().getAge() - obj2.getValue().getAge();
            }
        });
        System.out.println(""+arrayList.toString());
        for(int i = 0 ;i < arrayList.size(); i++){
            Entry<Integer, Student> entry = arrayList.get(i);
            linkedHashMap.put(entry.getKey(), entry.getValue());
        }
        return linkedHashMap;
    }
}

public class Student  implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 125252L;
    private transient String name = "姚明";
    private int age;
    
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}
复制代码

上面中最重要的是理解下面的几个思路:

1、   返回值是HashMap,该hashmap具有输出排序的功能,只能想到链接结构可以实现排序,想到hashmap的子类LinkedHashMap。

2、   第二个很关键的是要实现自定义排序,第一想到的肯定是集合框架类的Collections的sort方法,该方法可以实现自定义排序

3、   第三个是Collections只能实现对集合框架类排序,list set属于Collection集合,map属于了另外一个框架

4、   那些要将hashmap转化成list集合才能进行排序,hashmap的成员函数entrySet可以将hashmap转化成set集合,利用set集合可以构造list集合

5、   对list集合进行排序

6、   将排序后的list集合存储在LinkedhashMap中,返回。

上面这个面试题对整个集合框架类启到了一个很好的复习的作用

posted on   luzhouxiaoshuai  阅读(686)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示