treemap倒叙_TreeMap 排序

一、TreeMap

TreeMap 默认排序规则:按照key的字典顺序来排序(升序)

当然,也可以自定义排序规则:要实现Comparator接口。

用法简单,先看下下面的demo

public classSortDemo {public static voidmain(String[] args) {

System.out.println("---------------- 默认 排序结果-----------------");

createDefaultSortTreeMap();

System.out.println("---------------- 自定义 排序结果-----------------");

createDefinitionSortTreeMap();

}public static voidcreateDefaultSortTreeMap() {

TreeMap map = new TreeMap();

init(map);

print(map);

}public static voidcreateDefinitionSortTreeMap() {

TreeMap map = new TreeMap(new Comparator() {

@Overridepublic intcompare(String o1, String o2) {returno2.compareTo(o1);

}

});

init(map);

print(map);

}public static void init(Mapmap) {

map.put("c", "1");

map.put("a", "1");

map.put("bb", "1");

map.put("b", "1");

}public static void print(Mapmap) {

Iterator> it =map.entrySet().iterator();while(it.hasNext()) {

Entry entry =it.next();

System.out.println(entry.getKey()+ " : " +entry.getValue());

}

}

结果:---------------- 默认 排序结果-----------------a :1b :1bb :1c :1

---------------- 自定义 排序结果-----------------c :1bb :1b :1a :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" < "12" < "13"这种效果,如何实现呢?很简单,把Key改为Long类型,这样,就会按照大小来排序。

看下下面的例子,可能比较简单明了!

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public classSortDemo2 {2

3 private final static int SIZE = 30;4

5 public static voidmain(String[] args) {6 System.out.println("---------------- key 为 Sting 排序结果-----------------");7 String s = newString();8 createTreeMap(s);9 System.out.println("---------------- key 为 Long 排序结果-----------------");10 Long l = new Long(0);11 createTreeMap(l);12 }13

14 public static voidcreateTreeMap(Object obj) {15

16 TreeMap map = new TreeMap<>(new Comparator() {17

18 @Override19 public intcompare(Object o1, Object o2) {20 if(o1 instanceof String && o2 instanceofString) {21 return((String) o1).compareTo((String) o2);22 } else if(o1 instanceof Long && o2 instanceofLong) {23 return((Long) o1).compareTo((Long) o2);24 }25 return 0;26 }27

28 });29

30 for(int i = 1; i

39 print(map);40 }41

42 public static void print(Mapmap) {43 Iterator> it =map.entrySet().iterator();44 while(it.hasNext()) {45 Entry 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

View Code

你可能感兴趣的:(treemap倒叙)