一、TreeMap
TreeMap 默认排序规则:按照key的字典顺序来排序(升序)
当然,也可以自定义排序规则:要实现Comparator接口。
用法简单,先看下下面的demo

public class SortDemo {
public static void main(String[] args) {
System.out.println("---------------- 默认 排序结果-----------------");
createDefaultSortTreeMap();
System.out.println("---------------- 自定义 排序结果-----------------");
createDefinitionSortTreeMap();
}
public static void createDefaultSortTreeMap() {
TreeMap<String, String> map = new TreeMap<String, String>();
init(map);
print(map);
}
public static void createDefinitionSortTreeMap() {
TreeMap<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
init(map);
print(map);
}
public static void init(Map<String, String> map) {
map.put("c", "1");
map.put("a", "1");
map.put("bb", "1");
map.put("b", "1");
}
public static void print(Map<String, String> map) {
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while(it.hasNext()) {
Entry<String, String> entry = it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
结果:
---------------- 默认 排序结果-----------------
a : 1
b : 1
bb : 1
c : 1
---------------- 自定义 排序结果-----------------
c : 1
bb : 1
b : 1
a : 1

二、扩展:字典顺序
1、排序规则
两个字符串 s1, s2比较
(1)、如果s1和s2是父子串关系,则 子串 < 父串
(2)、如果非为父子串关系, 则从第一个非相同字符来比较。
例子 s1 = "ab", s2 = "ac" 这种情况算法规则是从第二个字符开始比较,由于'b' < 'c' 所以 "ab" < "ac"
(3)、字符间的比较,是按照字符的字节码(ascii)来比较
2、 compareTo 实现机制:对于字符串来说,字典排序规则;对于数字来说,直接按照大小排序
下面, 是我在项目中,遇到的一个坑,也不能算坑吧,只能说基础掌握得不扎实,导致老不断犯错。先说下场景,有个需求要对Map排序,当时想当然就用了自定义的TreeMap(new
Comparator )
key 为 String, value 也会String类型, 然后很不幸的是,我的Key 是 数字 字符串 ,如 Map.put("2","1"),Map.put("12","1"),Map.put("13","1")
正常思维排序结果是 "2" < "12" < "13" ,仔细一想,compareTo 底层算法是 "字典排序",正确的排序结果 : "12" < "13" < "2"
但是我的需求又是想要"2" < "12" < "13"这种效果,如何实现呢?很简单,把Key改为Long类型,这样,就会按照大小来排序。
看下下面的例子,可能比较简单明了!


1 public class SortDemo2 {
2
3 private final static int SIZE = 30;
4
5 public static void main(String[] args) {
6 System.out.println("---------------- key 为 Sting 排序结果-----------------");
7 String s = new String();
8 createTreeMap(s);
9 System.out.println("---------------- key 为 Long 排序结果-----------------");
10 Long l = new Long(0);
11 createTreeMap(l);
12 }
13
14 public static void createTreeMap(Object obj) {
15
16 TreeMap<Object, Object> map = new TreeMap<>(new Comparator<Object>() {
17
18 @Override
19 public int compare(Object o1, Object o2) {
20 if(o1 instanceof String && o2 instanceof String) {
21 return ((String) o1).compareTo((String) o2);
22 } else if(o1 instanceof Long && o2 instanceof Long) {
23 return ((Long) o1).compareTo((Long) o2);
24 }
25 return 0;
26 }
27
28 });
29
30 for(int i = 1; i<SIZE; i++) {
31 if(obj instanceof String) {
32 map.put(String.valueOf(i), String.valueOf(i));
33 }
34 if(obj instanceof Long) {
35 map.put(Long.valueOf(i), Long.valueOf(i));
36 }
37 }
38
39 print(map);
40 }
41
42 public static void print(Map<Object, Object> map) {
43 Iterator<Entry<Object, Object>> it = map.entrySet().iterator();
44 while(it.hasNext()) {
45 Entry<Object, Object> entry = it.next();
46 System.out.println(entry.getKey() + " : " + entry.getValue());
47 }
48 }
49 }
50
51 结果:
52
53 ---------------- key 为 Sting 排序结果-----------------
54 1 : 1
55 10 : 10
56 11 : 11
57 12 : 12
58 13 : 13
59 14 : 14
60 15 : 15
61 16 : 16
62 17 : 17
63 18 : 18
64 19 : 19
65 2 : 2
66 20 : 20
67 21 : 21
68 22 : 22
69 23 : 23
70 24 : 24
71 25 : 25
72 26 : 26
73 27 : 27
74 28 : 28
75 29 : 29
76 3 : 3
77 4 : 4
78 5 : 5
79 6 : 6
80 7 : 7
81 8 : 8
82 9 : 9
83 ---------------- key 为 Long 排序结果-----------------
84 1 : 1
85 2 : 2
86 3 : 3
87 4 : 4
88 5 : 5
89 6 : 6
90 7 : 7
91 8 : 8
92 9 : 9
93 10 : 10
94 11 : 11
95 12 : 12
96 13 : 13
97 14 : 14
98 15 : 15
99 16 : 16
100 17 : 17
101 18 : 18
102 19 : 19
103 20 : 20
104 21 : 21
105 22 : 22
106 23 : 23
107 24 : 24
108 25 : 25
109 26 : 26
110 27 : 27
111 28 : 28
112 29 : 29
https://www.cnblogs.com/chenmo-xpw/p/4922641.html

被折叠的 条评论
为什么被折叠?



