HashMap与TreeMap按照key和value排序

时间:2021-04-01 19:16:38
  1 package com.sort;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.TreeMap;
11
12 /**
13 * Map的排序
14 *
15 * @author root
16 *
17 */
18 public class MapSort {
19 /**
20 * TreeMap按照key进行排序
21 */
22 public static void TreeMapSortByKey() {
23 Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
24 @Override
25 public int compare(String o1, String o2) {
26 // 降序排列
27 return o2.compareTo(o1);
28 }
29 });
30 map.put("c", "ccccc");
31 map.put("a", "aaaaa");
32 map.put("b", "bbbbb");
33 map.put("d", "ddddd");
34 for (String key : map.keySet()) {
35 System.err.println("key:" + key + " value:" + map.get(key));
36 }
37 }
38
39 /**
40 * TreeMap按照value排序
41 */
42 public static void TreeMapSortByValue() {
43 Map<String, String> map = new TreeMap<String, String>();
44 map.put("d", "ccccc");
45 map.put("b", "bbbbb");
46 map.put("a", "eeeee");
47 map.put("c", "ddddd");
48 // 将map.entrySet()转换成list
49 List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
50 // 通过比较器来实现排序
51 Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
52 @Override
53 public int compare(Entry<String, String> o1, Entry<String, String> o2) {
54 // 升序排序
55 return o1.getValue().compareTo(o2.getValue());
56 }
57 });
58 for (Map.Entry<String, String> mapping : list) {
59 System.out.println(mapping.getKey() + ":" + mapping.getValue());
60 }
61 }
62
63 /**
64 * Map按照Key排序
65 */
66 public static void MapSortByKey() {
67 Map<String, String> map = new HashMap<String, String>();
68 map.put("d", "ccccc");
69 map.put("b", "bbbbb");
70 map.put("a", "eeeee");
71 map.put("c", "ddddd");
72 // 将map.entrySet()转换成list
73 List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
74 // 通过比较器来实现排序
75 Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
76 @Override
77 public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
78 // 升序排序
79 return o1.getKey().compareTo(o2.getKey());
80 }
81 });
82 for (Map.Entry<String, String> mapping : list) {
83 System.out.println(mapping.getKey() + ":" + mapping.getValue());
84 }
85 }
86
87 /**
88 * Map按照Value排序
89 */
90 public static void MapSortByValue() {
91 Map<String, String> map = new HashMap<String, String>();
92 map.put("d", "ccccc");
93 map.put("b", "bbbbb");
94 map.put("a", "eeeee");
95 map.put("c", "ddddd");
96 // 将map.entrySet()转换成list
97 List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
98 // 通过比较器来实现排序
99 Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
100 @Override
101 public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
102 // 降序排序
103 return o2.getValue().compareTo(o1.getValue());
104 }
105 });
106 for (Map.Entry<String, String> mapping : list) {
107 System.out.println("key:"+mapping.getKey() + " value:" + mapping.getValue());
108 }
109 }
110
111 public static void main(String[] args) {
112 // MapSort.TreeMapSortByKey();
113 // MapSort.TreeMapSortByValue();
114 MapSort.MapSortByKey();
115 MapSort.MapSortByValue();
116 }
117 }