JAVA 常用 Map

  • 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 k = (Comparable) 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 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 extends HashMap.Node {
        Entry before, after;
        Entry(int hash, K key, V value, Node next) {
            super(hash, key, value, next);
        }
    }

    /**
     * The head (eldest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry head;

    /**
     * The tail (youngest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry tail;

WeakHashMap

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

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

IdentityHashMap

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

val map = new IdentityHashMap();

        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)