Map排序-按Key,按Value(转)

  1. TreeMap默认按照key递增排序
public class Main {

    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        TreeMap<String, Integer> treeMap1 = new TreeMap<>();
        treeMap.put(7, "h");
        treeMap.put(8, "g");
        treeMap.put(9, "f");
        treeMap.put(10, "e");
        treeMap.put(14, "a");
        treeMap.put(1, "w");
        treeMap.put(2, "v");
        treeMap.put(3, "u");
        treeMap.put(11, "d");
        treeMap.put(12, "c");
        treeMap.put(13, "b");
        treeMap.put(4, "k");
        treeMap.put(5, "j");
        treeMap.put(6, "i");
        System.out.println("----------------------*------------------------------");
        while (treeMap.size() != 0) {
            //treemap自动按照key进行递增排序
            System.out.println(treeMap.firstEntry().getKey() + " - " + treeMap.firstEntry().getValue());
            treeMap1.put(treeMap.firstEntry().getValue(), treeMap.firstEntry().getKey());
            treeMap.remove(treeMap.firstKey());
        }
        System.out.println("----------------------*------------------------------");
        while (treeMap1.size() != 0) {
            //treemap自动按照key进行递增排序
            System.out.println(treeMap1.firstEntry().getKey() + " - " + treeMap1.firstEntry().getValue());
            treeMap1.remove(treeMap1.firstKey());
        }
        System.out.println("----------------------*------------------------------");
    }
}

 

得到结果:

----------------------*------------------------------
1 - w
2 - v
3 - u
4 - k
5 - j
6 - i
7 - h
8 - g
9 - f
10 - e
11 - d
12 - c
13 - b
14 - a
----------------------*------------------------------
a - 14
b - 13
c - 12
d - 11
e - 10
f - 9
g - 8
h - 7
i - 6
j - 5
k - 4
u - 3
v - 2
w - 1
----------------------*------------------------------

 

 

2.按照value排序

public class Main {

    public static void main(String[] args) {
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        treeMap.put("s", 2);
        treeMap.put("w", 5);
        treeMap.put("d", 1);
        treeMap.put("f", 0);
        treeMap.put("h", 9);
        treeMap.put("q", 22);
        treeMap.put("a", 25);
        //按照value排序
        List<Map.Entry<String, Integer>> entryArrayList = new ArrayList<>(treeMap.entrySet());
        Collections.sort(entryArrayList, Comparator.comparing(Map.Entry::getValue));
        System.out.println("----------------------*------------------------------");
        for (Map.Entry<String, Integer> entry : entryArrayList) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
        System.out.println("----------------------*------------------------------");

    }
}

 

结果:

----------------------*------------------------------
f - 0
d - 1
s - 2
w - 5
h - 9
q - 22
a - 25
----------------------*------------------------------

 

 

注意:

List<Map.Entry<String, Integer>> entryArrayList = new ArrayList<>(treeMap.entrySet());
        Collections.sort(entryArrayList, Comparator.comparing(Map.Entry::getValue));

 

 

等价于下面的lamda:

List<Map.Entry<String, Integer>> entryArrayList = new ArrayList<>(treeMap.entrySet());
        Collections.sort(entryArrayList, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));

 

也等价于:

List<Map.Entry<String, Integer>> entryArrayList = new ArrayList<>(treeMap.entrySet());
        Collections.sort(entryArrayList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });

 转自:https://blog.csdn.net/u011734144/article/details/52384284

 
  1. public class Testing {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         HashMap<String,Long> map = new HashMap<String,Long>();  
  6.         ValueComparator bvc =  new ValueComparator(map);  
  7.         TreeMap<String,Long> sorted_map = new TreeMap<String,Long>(bvc);  
  8.   
  9.         map.put("A",99);  
  10.         map.put("B",67);  
  11.         map.put("C",67);  
  12.         map.put("D",67);  
  13.   
  14.         System.out.println("unsorted map: "+map);  
  15.   
  16.         sorted_map.putAll(map);  
  17.   
  18.         System.out.println("results: "+sorted_map);  
  19.     }  
  20. }  
  21.   
  22. class ValueComparator implements Comparator<String> {  
  23.   
  24.     Map<String, Long> base;  
  25.     //这里需要将要比较的map集合传进来
  26.     public ValueComparator(Map<String, Long> base) {  
  27.         this.base = base;  
  28.     }  
  29.   
  30.     // Note: this comparator imposes orderings that are inconsistent with equals.    
  31.     //比较的时候,传入的两个参数应该是map的两个key,根据上面传入的要比较的集合base,可以获取到key对应的value,然后按照value进行比较   
  32.     public int compare(String a, String b) {  
  33.         if (base.get(a) >= base.get(b)) {  
  34.             return -1;  
  35.         } else {  
  36.             return 1;  
  37.         } // returning 0 would merge keys  
  38.     }  
  39. }  
posted @ 2018-07-13 11:38  annofyf  阅读(262)  评论(0编辑  收藏  举报