欢迎您的访问
专注于分享最有价值的互联网技术干货

HashMap、HashTable、LinkedHashMap和TreeMap用法和区别

几个T的资料等你来白嫖
双倍快乐
资源帮找!!!

Java为数据结构中的映射定义了一个接口java.util.Map,它有四个实现类,分别是HashMap、HashTable、LinkedHashMap和TreeMap。本节实例主要介绍这4中实例的用法和区别。
关键技术剖析:
Map用于存储键值对,根据键得到值,因此不允许键重复,值可以重复。
l (1)HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为null,不允许多条记录的值为null。HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
l (2)Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢。
l (3)LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的。在遍历的时候会比HashMap慢。有HashMap的全部特性。
l (4)TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iteraor遍历TreeMap时,得到的记录是排过序的。TreeMap的键和值都不能为空。

TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。
LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同

代码如下:

public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "Level 1");
        map.put("2", "Level 2");
        map.put("3", "Level 3");
        map.put("a", "Level a");
        map.put("b", "Level b");
        map.put("c", "Level c");
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> e = it.next();
            System.out.println("Key: " + e.getKey() + ";   Value: " + e.getValue());
        }
        System.out.println("======================================");
        Map<String, String> map1 = new TreeMap<String, String>();
        map1.put("1", "Level 1");
        map1.put("2", "Level 2");
        map1.put("3", "Level 3");
        map1.put("a", "Level a");
        map1.put("b", "Level b");
        map1.put("c", "Level c");
        Iterator<Map.Entry<String, String>> it1 = map1.entrySet().iterator();
        while (it1.hasNext()) {
            Map.Entry<String, String> e1 = it1.next();
            System.out.println("Key: " + e1.getKey() + ";   Value: " + e1.getValue());
        }
        System.out.println("======================================");
        Map<String, String> map2 = new LinkedHashMap<>();
        map2.put("1", "Level 1");
        map2.put("2", "Level 2");
        map2.put("3", "Level 3");
        map2.put("a", "Level a");
        map2.put("b", "Level b");
        map2.put("c", "Level c");
        Iterator<Map.Entry<String, String>> it2 = map1.entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<String, String> e2 = it2.next();
            System.out.println("Key: " + e2.getKey() + ";   Value: " + e2.getValue());
        }
    }

运行结果:

Key: 3; Value: Level 3
Key: 2; Value: Level 2
Key: 1; Value: Level 1
Key: b; Value: Level b
Key: c; Value: Level c
Key: a; Value: Level a
======================================
Key: 1; Value: Level 1
Key: 2; Value: Level 2
Key: 3; Value: Level 3
Key: a; Value: Level a
Key: b; Value: Level b
Key: c; Value: Level c
======================================
Key: 1; Value: Level 1
Key: 2; Value: Level 2
Key: 3; Value: Level 3
Key: a; Value: Level a
Key: b; Value: Level b
Key: c; Value: Level c

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系我们:大白菜博客 » HashMap、HashTable、LinkedHashMap和TreeMap用法和区别

评论 抢沙发

2 + 7 =
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏