对Map中数据,按value值排序方法
2012-12-24 13:45
381 查看
1.Map<String,Integer>类型
2.Map<String,Float>类型
3.Map<String,Double>类型
//声明 Map<String,Integer> hashMap = new HashMap<String,Integer>(); //向Map中添加数据 //..... //转换 ArrayList<Entry<String, Integer>> arrayList = new ArrayList<Entry<String, Integer>>( hashMap.entrySet()); //排序 Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> map1, Map.Entry<String, Integer> map2) { return (map2.getValue() - map1.getValue()); } }); //输出 for (Entry<String, Integer> entry : arrayList) { System.out.println(entry.getKey() + "\t" + entry.getValue()); }
2.Map<String,Float>类型
//声明 Map<String,Float> hashMap = new HashMap<String,Float>(); //向Map中添加数据 //..... //转换 ArrayList<Entry<String, Float>> arrayList = new ArrayList<Map.Entry<String,Float>>(hashMap.entrySet()); //排序 Collections.sort(arrayList, new Comparator<Map.Entry<String, Float>>(){ public int compare(Map.Entry<String, Float> map1, Map.Entry<String,Float> map2) { return ((map2.getValue() - map1.getValue() == 0) ? 0 : (map2.getValue() - map1.getValue() > 0) ? 1 : -1); } }); //输出 for (Entry<String, Float> entry : arrayList) { System.out.println(entry.getKey() + "\t" + entry.getValue()); }
3.Map<String,Double>类型
//声明 Map<String,Double> hashMap = new HashMap<String,Double>(); //向Map中添加数据 //..... //转换 ArrayList<Entry<String, Double>> arrayList = new ArrayList<Map.Entry<String,Double>>(hashMap.entrySet()); //排序 Collections.sort(arrayList, new Comparator<Map.Entry<String, Double>>(){ public int compare(Map.Entry<String, Double> map1, Map.Entry<String,Double> map2) { return ((map2.getValue() - map1.getValue() == 0) ? 0 : (map2.getValue() - map1.getValue() > 0) ? 1 : -1); } }); //输出 for (Entry<String, Double> entry : arrayList) { System.out.println(entry.getKey() + "\t" + entry.getValue()); }
相关文章推荐
- 对Map中数据,按value值排序方法
- 对Map中数据,按value值排序方法
- 获取map中根据key进行排序的value数据
- 对map的value进行排序的方法
- Map四种获取key和value值的方法,以及对map中的元素排序
- Map四种获取key和value值的方法,以及对map中的元素排序
- 四种Map获取key和value值的方法,及对map中的元素进行排序
- 遍历取出Map集合key-value数据的4种方法
- Map排序,获取map的第一值,根据value取key等操作(数据预处理)
- Map四种获取key和value值的方法,以及对map中的元素排序
- Map四种获取key和value值的方法,以及对map中的元素排序(转载)
- Java Map 按照Value排序的实现方法
- Java Map 按key排序和按Value排序的实现方法
- 使用XML的value()方法将多行数据合并成一列
- C++按map的value进行排序
- 利用Comparator根据多个Map中某个共同属性的value值对其进行排序
- Map.containsKey/Map.containsValue方法——判断Map集合对象中是否包含指定的键名
- (java)根据map中value进行排序
- Java 对Map进行排序的实现方法
- IOS 中给数据排序的两种方法---持续更新中---