zoukankan      html  css  js  c++  java
  • HashMap、HashTable、LinkedHashMap和TreeMap用法和区别

    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

  • 相关阅读:
    select top 变量问题
    distinct top執行順序
    Subquery typo with using in(转)
    sql:查询课程号'0312091006'成绩排名第5到第10之间的学生学号
    case when then
    触发器
    索引
    管理事物处理
    053345
    053344
  • 原文地址:https://www.cnblogs.com/cuixiaomeng/p/7747683.html
Copyright © 2011-2022 走看看