HashMap排序
来源:互联网 发布:做淘宝需要什么设备 编辑:程序博客网 时间:2024/06/14 12:45
publicstatic HashMap<Integer, User> sortHashMap(HashMap<Integer, User> map) {
// 首先拿到map的键值对集合
Set<Entry<Integer, User>> entrySet = map.entrySet();
// 将set转化成list集合 为什么 因为使用 工具类的排序方法
ArrayList<Entry<Integer, User>> list =new ArrayList<Entry<Integer, User>>(entrySet);
Collections.sort(list,new Comparator<Entry<Integer, User>>() {
@Override
publicintcompare(Entry<Integer, User> o1, Entry<Integer, User> o2) {
return o2.getValue().getAge() - o1.getValue().getAge();
}
});
// 创建一个有序的HashMap集合
LinkedHashMap<Integer, User> linkedHashMap =new LinkedHashMap<Integer, User>();
//将list集合中的数据存储在LinkedHashMap 里
for(Entry<Integer,User> entry:list){
linkedHashMap.put(entry.getKey(), entry.getValue());
}
return linkedHashMap;
}
- HashMap排序
- HashMap排序
- HashMap排序
- HashMap排序
- HashMap 排序
- HashMap排序
- HashMap排序
- hashmap排序
- hashmap 排序
- hashmap 排序
- HashMap排序
- HashMap排序
- hashMap排序
- HashMap排序
- HashMap排序
- HashMap排序
- hashmap排序
- HashMap排序
- LODOP WEB项目打印功能
- 多线程(二)--同步及死锁
- ST下载提示No Cortex-M SW Device Found
- Go语言开发常见陷阱,你遇到过几个?
- 链接
- HashMap排序
- 【转】公司危机、下岗困局、不受重视,程序员该如何面对职场挫折?
- 判断两个链表是否相交,若相交,求交点:1.不带环。2.有可能带环
- Linux内核 设备树操作常用API
- 算法分析与设计——LeetCode Problem.11 Container With Most Water
- Jenkins简介
- Java8内存模型—永久代(PermGen)和元空间(Metaspace)
- Android评论功能
- jQuery学习笔记(二)_选择器