对HashMap中的值进行排序
2016-04-13 20:56
399 查看
public static void main(String[] args) {
}
class ValueComparator implements Comparator {
HashMap<String, Double> map = new HashMap<String, Double>(); ValueComparator bvc = new ValueComparator(map); TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc); map.put("A", 98.5); map.put("B", 85.0); map.put("C", 67.4); map.put("D", 67.3); System.out.println("unsorted map: " + map); sorted_map.putAll(map); System.out.println("results: " + sorted_map); }
}
class ValueComparator implements Comparator {
Map<String, Double> base; public ValueComparator(Map<String, Double> base) { this.base = base; } // Note: this comparator imposes orderings that are inconsistent with equals. public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return -1; } else { return 1; } // returning 0 would merge keys }
相关文章推荐
- CSS选择器详解一
- Android-view自定义-Canvas
- 回溯水题——置棋问题
- 【SDOI2014】数数
- poj 3259 Wormholes 最短路(Bellman_Ford)
- C#中的代码书写规范以及命名规范
- http协议详解之url篇
- ANR Warning,RemoteViews can only be used once ,if not 问题
- 几个性能测试工具
- c++学习之同名隐藏
- 大牛们的博客
- Bellman-Ford算法模板
- shell脚本学习笔记 (流编辑器sed)
- 素数环
- 南阳题目364-田忌赛马
- 几个性能测试工具
- C#读取Excel数据到数组
- c++中的日期和时间
- 匈牙利命名法
- POJ 2688 Cleaning Robot(状压dp+bfs)