LinkedHashMap, HashMap以及TreeHashMap的比较

转自: https://blog.csdn.net/alanjager/article/details/53426681

首先说说相同点,LinkedHashMap, HashMap和TreeHashMap都implement Map interface,从功能性上来说基本上是相同的。

不同点:

1. HashMap不保证元素的顺序,即添加的HashMap的顺序和储存在HashMap内的顺序是不一致的

2. TreeMap中的元素遵循natural order,使用compareTo()方法使内容按照key有序

3. LinkedHashMap保证元素的结果和添加元素的顺序一致

[java]  view plain  copy
  1. public class SeveralMaps {  
  2.     public static void main(String[] args) {  
  3.         Map hashMap = new HashMap();  
  4.         hashMap.put("6""6");  
  5.         hashMap.put("1""1");  
  6.         hashMap.put("2""2");  
  7.         hashMap.put("5""5");  
  8.         hashMap.put("3""3");  
  9.         hashMap.put("4""4");  
  10.         System.out.println("*********** HashMap *************");  
  11.         System.out.print(hashMap.keySet());  
  12.         System.out.println(hashMap.values());  
  13.   
  14.         Map treeMap = new TreeMap();  
  15.         treeMap.put("6""6");  
  16.         treeMap.put("1""1");  
  17.         treeMap.put("2""2");  
  18.         treeMap.put("5""5");  
  19.         treeMap.put("3""3");  
  20.         treeMap.put("4""4");  
  21.         System.out.println("*********** TreeMap *************");  
  22.         System.out.print(treeMap.keySet());  
  23.         System.out.println(treeMap.values());  
  24.   
  25.   
  26.         Map linkedHashMap = new LinkedHashMap();  
  27.         linkedHashMap.put("6""6");  
  28.         linkedHashMap.put("1""1");  
  29.         linkedHashMap.put("2""2");  
  30.         linkedHashMap.put("5""5");  
  31.         linkedHashMap.put("3""3");  
  32.         linkedHashMap.put("4""4");  
  33.         System.out.println("*********** LinkedHashMap *************");  
  34.         System.out.print(linkedHashMap.keySet());  
  35.         System.out.println(linkedHashMap.values());  
  36.     }  
  37. }  

代码的运行结果为

[plain]  view plain  copy
  1. *********** HashMap *************  
  2. [1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]  
  3. *********** TreeMap *************  
  4. [1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]  
  5. *********** LinkedHashMap *************  
  6. [6, 1, 2, 5, 3, 4][6, 1, 2, 5, 3, 4]  


在StackOverflow上有人总结了一个对比的表格,如下

╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗
║   Property   ║       HashMap       ║      TreeMap      ║     LinkedHashMap    ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║  no guarantee order ║ sorted according  ║                      ║
║   Order      ║ will remain constant║ to the natural    ║    insertion-order   ║
║              ║      over time      ║    ordering       ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║  Get/put     ║                     ║                   ║                      ║
║   remove     ║        Θ (1)        ║      Θ (log(n))   ║         Θ (1)        ║
║ containsKey  ║                     ║                   ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║                     ║   NavigableMap    ║                      ║
║  Interfaces  ║         Map         ║       Map         ║         Map          ║
║              ║                     ║    SortedMap      ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║                     ║                   ║                      ║
║     Null     ║       allowed       ║    only values    ║       allowed        ║
║ values/keys  ║                     ║                   ║                      ║
╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣
║              ║   Fail-fast behavior of an iterator cannot be guaranteed       ║
║   Fail-fast  ║ impossible to make any hard guarantees in the presence of      ║
║   behavior   ║           unsynchronized concurrent modification               ║
╠══════════════╬═════════════════════╦═══════════════════╦══════════════════════╣
║              ║                     ║                   ║                      ║
║Implementation║      buckets        ║   Red-Black Tree  ║    double-linked     ║
║              ║                     ║                   ║       buckets        ║
╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣
║      Is      ║                                                                ║
║ synchronized ║              implementation is not synchronized                ║
╚══════════════╩════════════════════════════════════════════════════════════════╝
考虑多线程操作的话使用ConcurrentHashMap会有更高的效率

文章代码链接https://github.com/AlanJager/java_learning/blob/master/src/SeveralMaps.java


你可能感兴趣的:(LinkedHashMap, HashMap以及TreeHashMap的比较)