HashMap&HashTable&LinkedHashMap&TreeMap

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36564655/article/details/81564933

最近发现对Map类的理解还不够深刻 ,因此在这边赶紧总结mark一下,Map类还是非常有用滴~

  • (1)HashMap与HashTable:

   相同点:这两者都存储的键值对是无序的

   不同点:HashMap线程不安全;HashTable线程不安全;

                 HashMap支持key或value的值为Null;Hashtable不支持key或value的值为Null,非法

 一般现在不建议用HashTable,因为:
①HashTable是HashTable是遗留类,内部实现很多没优化和冗余。
②即使在多线程环境下,现在也有同步的ConcurrentHashMap替代,没有必要因为是多线程而用HashTable。

  • (2)LinkedHashMap&TreeMap

linkedHashMap可以实现按照插入顺序或访问顺序进行排序,是HashMap的子类,内部多了双向链表来保存顺序

treeMap按照自然顺序或者比较器自定义实现来排序,采用红黑树实现,红黑树的中序遍历是有序的

  • package DEMO;
    
    import java.util.*;
    
    //linkedHashMap可以实现按照插入顺序和访问顺序进行排序,是HashMap的子类,内部多了双向链表来保存顺序
    //treeMap按照自然顺序或者比较器自定义实现来排序
    public class HashMapDemo {
        public static void main(String[] args) {
            //HashMap支持key或value的值为Null
            //注意:HashMap只允许一个key为null
            HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
            hashMap.put(null, null);
            hashMap.put(null, 2);
            System.out.println(hashMap.get(null));
            //Hashtable不支持key或value的值为Null,非法
            /*
            Hashtable<String,Integer> hashtable = new Hashtable<>();
            hashtable.put(null,2);
            System.out.println(hashtable.containsKey(null));
            */
            //LinkedMap
            Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("b", 7);
            linkedHashMap.put("c", 2);
            linkedHashMap.put("a", 3);
            linkedHashMap.put("e", 6);
            Iterator it = linkedHashMap.entrySet().iterator();
            System.out.println("=============linkedHashMap:插入顺序==============");
            while (it.hasNext())
                System.out.println(it.next());//我们可以看到,其输出顺序是完成按照插入顺序的
           //按照访问顺序访问,最先返回最久没有访问过的。如果使用了put/get操作,将对应的键值对放到末尾(末尾存储最近被访问过的)
            Map<String, String> linkedmap2 = new LinkedHashMap<String, String>(16, 0.75f, true);
            linkedmap2.put("apple", "苹果");
            linkedmap2.put("watermelon", "西瓜");
            linkedmap2.put("banana", "香蕉");
            linkedmap2.put("peach", "桃子");
            linkedmap2.get("apple");
            Iterator iter = linkedmap2.entrySet().iterator();
            System.out.println("=============LinkedHashMap:访问顺序==============");
            while (iter.hasNext()) {
                System.out.println(iter.next());
            }
            //TreeMap,默认按照自然顺序排序
            Map<String, Integer> treeMap = new TreeMap<>();
            treeMap.put("3", 7);
            treeMap.put("4", 2);
            treeMap.put("1", 3);
            treeMap.put("5", 6);
            it = treeMap.entrySet().iterator();
            System.out.println("=============treeMap:升序==============");
            while (it.hasNext())
                System.out.println(it.next());
    
        }
    }
    

    更多参考链接: 

  • http://yikun.github.io/2015/04/06/Java-TreeMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/

猜你喜欢

转载自blog.csdn.net/weixin_36564655/article/details/81564933