LinkedHashMap, HashMap以及TreeHashMap的比较

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

不同点:

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

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

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

public class SeveralMaps {
    public static void main(String[] args) {
        Map hashMap = new HashMap();
        hashMap.put("6", "6");
        hashMap.put("1", "1");
        hashMap.put("2", "2");
        hashMap.put("5", "5");
        hashMap.put("3", "3");
        hashMap.put("4", "4");
        System.out.println("*********** HashMap *************");
        System.out.print(hashMap.keySet());
        System.out.println(hashMap.values());

        Map treeMap = new TreeMap();
        treeMap.put("6", "6");
        treeMap.put("1", "1");
        treeMap.put("2", "2");
        treeMap.put("5", "5");
        treeMap.put("3", "3");
        treeMap.put("4", "4");
        System.out.println("*********** TreeMap *************");
        System.out.print(treeMap.keySet());
        System.out.println(treeMap.values());


        Map linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("6", "6");
        linkedHashMap.put("1", "1");
        linkedHashMap.put("2", "2");
        linkedHashMap.put("5", "5");
        linkedHashMap.put("3", "3");
        linkedHashMap.put("4", "4");
        System.out.println("*********** LinkedHashMap *************");
        System.out.print(linkedHashMap.keySet());
        System.out.println(linkedHashMap.values());
    }
}

代码的运行结果为

*********** HashMap *************
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
*********** TreeMap *************
[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6]
*********** LinkedHashMap *************
[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



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值