java Map 按值排序
2016-06-21 10:05
459 查看
/** * Sort map by value * * @param map * The map to be sorted * @return The sorted map by value */ public static Map<String, Integer> sort(Map<String, Integer> map) { List<Map.Entry<String, Integer>> infoIds = new ArrayList<>(map.entrySet()); // Sort by DESC Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o2.getValue() - o1.getValue()); } }); Map<String, Integer> newMap = new LinkedHashMap<>(); for (int i = 0; i < infoIds.size(); i++) { Entry<String, Integer> id = infoIds.get(i); newMap.put(id.getKey(), id.getValue()); } return newMap; }
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- 在命令行用 sort 进行排序
- Python中map()函数浅析
- Android使用Google Map浅谈
- 文件遍历排序函数
- 关于C#中排序函数的总结
- C#选择排序法实例分析
- C#插入法排序算法实例分析
- C#实现Datatable排序的方法
- MYSQL必知必会读书笔记第五章之排序检索数据
- SQLSERVER的排序问题结果不是想要的
- Ruby实现插入排序算法及进阶的二路插入排序代码示例
- Windows Powershell排序和分组管道结果
- C#通过IComparable实现ListT.sort()排序
- C#选择法排序实例分析
- SQL学习笔记四 聚合函数、排序方法
- C#对list列表进行随机排序的方法
- jQuery拖动元素并对元素进行重新排序
- 将MySQL查询结果按值排序的简要教程