Map按照Value值进行排序
来源:互联网 发布:淘宝的评论在哪里 编辑:程序博客网 时间:2024/05/09 19:10
1 TreeMap按照value进行排序
- public class Testing {
- public static void main(String[] args) {
- 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",99.5);
- map.put("B",67.4);
- 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<String> {
- 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
- }
- }
输出结果
- unsorted map: {D=67.3, A=99.5, B=67.4, C=67.4}
- results: {D=67.3, B=67.4, C=67.4, A=99.5}
2.HashMap按值进行排序
- public class MapUtil
- {
- public static <K, V extends Comparable<? super V>> Map<K, V>
- sortByValue( Map<K, V> map )
- {
- List<Map.Entry<K, V>> list =
- new LinkedList<Map.Entry<K, V>>( map.entrySet() );
- Collections.sort( list, new Comparator<Map.Entry<K, V>>()
- {
- public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
- {
- return (o1.getValue()).compareTo( o2.getValue() );
- }
- } );
- Map<K, V> result = new LinkedHashMap<K, V>();
- for (Map.Entry<K, V> entry : list)
- {
- result.put( entry.getKey(), entry.getValue() );
- }
- return result;
- }
- }
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("d", 2);
- map.put("c", 1);
- map.put("b", 1);
- map.put("a", 3);
- List<Map.Entry<String, Integer>> infoIds =
- new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
- //排序前
- for (int i = 0; i < infoIds.size(); i++) {
- String id = infoIds.get(i).toString();
- System.out.println(id);
- }
- //d 2
- //c 1
- //b 1
- //a 3
- //排序
- 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());
- return (o1.getKey()).toString().compareTo(o2.getKey());
- }
- });
- //排序后
- for (int i = 0; i < infoIds.size(); i++) {
- String id = infoIds.get(i).toString();
- System.out.println(id);
- }
- //根据key排序
- //a 3
- //b 1
- //c 1
- //d 2
- //根据value排序
- //a 3
- //d 2
- //b 1
- //c 1
0 0
- Map按照Value值进行排序
- Map按照Value值进行排序
- Map按照Value值进行排序
- C++中实现对map按照value值进行排序
- Java中Map按照Value值进行排序
- Map按照key和value进行排序
- Map按照Value值排序
- map 按照value排序
- C++ map容器 按照value值排序
- C++ map容器 按照value值排序
- C++ map容器 按照value值排序
- C++ map容器 按照value值排序
- Map中按照value的大小进行排序
- 对map集合按照value从大到小进行排序
- MapReduce 按照Value值进行排序输出
- HashMap按照value值进行排序
- Java Map按照Value 排序
- java map按照value排序
- HDFS原理分析:基本概念
- 物理公式
- jvm工作原理
- opencv程序十八:运动目标检测之两帧差分法
- 汇编语言基本指令
- Map按照Value值进行排序
- 掉落的球体
- Vim 多窗口操作
- 均值滤波和中值滤波的java代码
- POJ 2492 并查集
- 三足鼎立(杭电2552)
- 杭电2020
- 黑马程序员—java基础学习--异常(exception)
- 逗号表达式的数学解释