美文网首页JAVA
JAVA 常用 Map

JAVA 常用 Map

作者: 交藤 | 来源:发表于2021-05-12 17:34 被阅读0次
  • HashMap
  • ConcurrentHashMap
  • TreeMap
  • LinkedHashMap
  • WeakHashMap
  • IdentityHashMap

HashMap

https://www.jianshu.com/p/fde06b74c1d4

ConcurrentHashMap

https://www.jianshu.com/p/fde06b74c1d4

TreeMap

非哈希表,为红黑树结构,即平衡二叉排序树。时间复杂度为 O(logN)
key 需要实现 Comparable 接口

  • TreeSet 使用了 TreeMap
    // 二叉排序树遍历
    Comparable<? super K> k = (Comparable<? super K>) key;
    do {
        parent = t;
        cmp = k.compareTo(t.key);
        if (cmp < 0)
            t = t.left;
        else if (cmp > 0)
            t = t.right;
        else
            return t.setValue(value);   // 值相同则覆盖并结束
    } while (t != null);
    
    // 新增节点
     Entry<K,V> e = new Entry<>(key, value, parent);
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
    
    // 平衡树
    fixAfterInsertion(e);

LinkedHashMap

继承自 HashMap 并重写 HashMap 中的方法,实现对链表的维护

  • LinkedHashSet 使用了 LinkedHashMap

   static class Entry<K,V> extends HashMap.Node<K,V> {
        Entry<K,V> before, after;
        Entry(int hash, K key, V value, Node<K,V> next) {
            super(hash, key, value, next);
        }
    }

    /**
     * The head (eldest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry<K,V> head;

    /**
     * The tail (youngest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry<K,V> tail;

WeakHashMap

Entry 继承自 WeakReference,通过查询 ReferenceQueue 移除被回收的 Entry
get、put、size 等方法均会触发对 ReferenceQueue 的查询与移除 Entry

    
    private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
        
        // 新建 Entry 时指定 ReferenceQueue,JVM 回收时会将该对象放入 queue
        Entry(Object key, V value,
              ReferenceQueue<Object> queue,
              int hash, Entry<K,V> next) {
            super(key, queue);
            this.value = value;
            this.hash  = hash;
            this.next  = next;
        }       
    }

IdentityHashMap

判断key值的相同,通过 == (即引用相同) 而非 equals
因此对于值相同但不同引用的 key,均会保存

val map = new IdentityHashMap<Object, String>();

        val a = new Object();
        val b = new Object();

        map.put(a, "a");
        map.put(b, "b");

        assert map.get(a) == "a";
        assert map.get(b) == "b";

        val c = a;
        map.put(c, "c");

        assert map.get(c) == map.get(a);
        assert map.get(c) == "c";

相关文章

  • JAVA 常用 Map

    HashMap ConcurrentHashMap TreeMap LinkedHashMap WeakHashM...

  • Map和Map.entry

    Map是java中的接口,Map.Entry是Map的一个内部接口。 Map提供了一些常用方法,如keySet()...

  • Map.Entry的用处

    java.util.Map.Entry是作为Map的一个内部接口用于遍历Map。Map提供了一些常用方法,如key...

  • Java中Map的原理

    在Java中常常需要使用Map作为存储工具。下面一一介绍各种Map的原理。 HashMap## Java中最常用的...

  • JUC学习笔记二

    HashMap HashMap类实现了Map接口,所以实现了Map常用的一些方法,Map通常在java开发中被称为...

  • Java面试

    一、Java基础: 1、常用的Java类?2、常用的集合,说出五个?3、IO中常用的类,说出五个?4、map是怎么...

  • 怎么在java 8的map中使用stream

    怎么在java 8的map中使用stream 简介 Map是java中非常常用的一个集合类型,我们通常也需要去遍历...

  • Java基础(二)

    Java要点2 JAVA 集合类 1.JAVA常用集合类功能、区别和性能 两大类:Collections,Map;...

  • Map接口

    Java常用集合为List,Set和Map,其中List和Set都实现了Collection接口,而Map并没有 ...

  • List、Set和Map

    java 常用集合list与Set、Map区别及适用场景总结Java中容器[Collection(List,Set...

网友评论

    本文标题:JAVA 常用 Map

    本文链接:https://www.haomeiwen.com/subject/tuagdltx.html