写支付签名的时候遇到了Map一家,就简单的比较了一下,于是乎先打印看看结果
Map<String,String> hashMap1 = new HashMap<>();
hashMap1.put("d","1");
hashMap1.put("e","2");
hashMap1.put("c","3");
hashMap1.put("b","4");
hashMap1.put("a","5");
for (Map.Entry<String, String> entry : hashMap1.entrySet()) {
System.out.println("hashMap1: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("------------------------------------------------------------------------------------------");
Map<String,String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("d","1");
linkedHashMap.put("e","2");
linkedHashMap.put("c","3");
linkedHashMap.put("b","4");
linkedHashMap.put("a","5");
for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
System.out.println("linkedHashMap: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("------------------------------------------------------------------------------------------");
Map<String,String> treeMap = new TreeMap<>();
treeMap.put("d","1");
treeMap.put("e","2");
treeMap.put("c","3");
treeMap.put("b","4");
treeMap.put("a","5");
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
System.out.println("treeMap: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
打印结果如下
hashMap1: Key = a, Value = 5
hashMap1: Key = b, Value = 4
hashMap1: Key = c, Value = 3
hashMap1: Key = d, Value = 1
hashMap1: Key = e, Value = 2
------------------------------------------------------------------------------------------
linkedHashMap: Key = d, Value = 1
linkedHashMap: Key = e, Value = 2
linkedHashMap: Key = c, Value = 3
linkedHashMap: Key = b, Value = 4
linkedHashMap: Key = a, Value = 5
------------------------------------------------------------------------------------------
treeMap: Key = a, Value = 5
treeMap: Key = b, Value = 4
treeMap: Key = c, Value = 3
treeMap: Key = d, Value = 1
treeMap: Key = e, Value = 2
乍一看 hashMap 与 treeMap貌似没啥区别,可能是值设置的太简单了存在偶然性,于是乎
改了一下
Map<String,Object> hashMap2 = new HashMap<String,Object>();
hashMap2.put("1b", "a");
hashMap2.put("2", "b");
hashMap2.put("4b", "d");
hashMap2.put("3", "c");
hashMap2.put("2b", "d");
hashMap2.put("3b", "c");
for(Map.Entry<String, Object> entry : hashMap2.entrySet()){
System.out.println("hashMap2: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("------------------------------------------------------------------------------------------");
Map<String,Object> linkedHashMap2 = new LinkedHashMap<>();
linkedHashMap2.put("1b", "a");
linkedHashMap2.put("2", "b");
linkedHashMap2.put("4b", "d");
linkedHashMap2.put("3", "c");
linkedHashMap2.put("2b", "d");
linkedHashMap2.put("3b", "c");
for(Map.Entry<String, Object> entry : linkedHashMap2.entrySet()){
System.out.println("linkedHashMap2: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
System.out.println("------------------------------------------------------------------------------------------");
Map<String,Object> treeMap2 = new TreeMap<>();
treeMap2.put("1b", "a");
treeMap2.put("2", "b");
treeMap2.put("4b", "d");
treeMap2.put("3", "c");
treeMap2.put("2b", "d");
treeMap2.put("3b", "c");
for(Map.Entry<String, Object> entry : treeMap2.entrySet()){
System.out.println("treeMap2: Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
打印结果果然不一样了
hashMap2: Key = 2b, Value = d
hashMap2: Key = 1b, Value = a
hashMap2: Key = 2, Value = b
hashMap2: Key = 3, Value = c
hashMap2: Key = 4b, Value = d
hashMap2: Key = 3b, Value = c
------------------------------------------------------------------------------------------
linkedHashMap2: Key = 1b, Value = a
linkedHashMap2: Key = 2, Value = b
linkedHashMap2: Key = 4b, Value = d
linkedHashMap2: Key = 3, Value = c
linkedHashMap2: Key = 2b, Value = d
linkedHashMap2: Key = 3b, Value = c
------------------------------------------------------------------------------------------
treeMap2: Key = 1b, Value = a
treeMap2: Key = 2, Value = b
treeMap2: Key = 2b, Value = d
treeMap2: Key = 3, Value = c
treeMap2: Key = 3b, Value = c
treeMap2: Key = 4b, Value = d
综上简单的来看,只有treeMap做了排序
hashMap看心情排序
linkedHashMap按照插入顺序排序
简单的理解就这么多,代码是写完了,有时间深入
欢迎留言,指正不足