zoukankan      html  css  js  c++  java
  • HashMap Hashtable LinkedHashMap TreeMap

    //      Map<String, String> map = new HashMap<String, String>();        // bb    aa    cc
            Map<String, String> map = new Hashtable<String, String>();      // bb    cc    aa
    //      Map<String, String> map = new LinkedHashMap<String, String>();  // aa    bb    cc
    //        Map<String, String> map = new TreeMap<String, String>();      // aa    bb    cc
            map.put("a3", "aa");
            map.put("a2", "bb");
            map.put("b1", "cc");
            for(Map.Entry<String, String> entry : map.entrySet()){
                String name = entry.getKey();
                Object score = entry.getValue();
                System.out.print(score+"	");  // bb    aa    cc
            }
    //        for (Iterator iterator = map.values().iterator(); iterator.hasNext();)     {
    //            String name = (String) iterator.next();
    //            System.out.print(name+"	"); // bb    aa    cc
    //        }
    
            /*
            总结归纳为:linkedMap在于存储数据你想保持进入的顺序与被取出的顺序一致的话,优先考虑LinkedMap,hashMap键只能允许为一条为空,
            value可以允许为多条为空,键唯一,但值可以多个。 经测试linkedMap键和值都不可以为空
    
            HashMap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。遍历时,取得数据的顺序是完全随机的。
                  HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null。HashMap不支持线程的同步(即任一时刻可以有多个线程同时写HashMap),
                  可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。
            Hashtable与 HashMap类似,它继承自Dictionary类。不同的是:它不允许记录的键或者值为空;它支持线程的同步(即任一时刻只有一个线程能写Hashtable),
                    因此也导致了 Hashtable在写入时会比较慢。
           LinkedHashMap保存了 插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。也可以在构造时带参数,按照应用次数排序。
                    在遍历的时候会比HashMap慢,不过有种情况例外:当HashMap容量很大,实际数据较少时,遍历起来可能会比LinkedHashMap慢。因
                    为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关。
            TreeMap实现SortMap接口,能够把它保存的记录 根据 key 排序。
                    默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。
            */
  • 相关阅读:
    Educational Codeforces Round 83 --- F. AND Segments
    Educational Codeforces Round 83 --- G. Autocompletion
    SEERC 2019 A.Max or Min
    2019-2020 ICPC Southwestern European Regional Programming Contest(Gym 102501)
    Educational Codeforces Round 78 --- F. Cards
    今天我学习了一门全新的语言
    codeforces 1323D 题解(数学)
    Educational Codeforces Round 80 (Div. 2) 题解 1288A 1288B 1288C 1288D 1288E
    Educational Codeforces Round 81 (Div. 2) 题解 1295A 1295B 1295C 1295D 1295E 1295F
    Codeforces Round #617 (Div. 3) 题解 1296C 1296D 1296E 1296F
  • 原文地址:https://www.cnblogs.com/hahajava/p/9560457.html
Copyright © 2011-2022 走看看