Java集合之 Map

84 阅读12分钟

Map 接口定义的集合又称为查找表,用于存储所谓"key-value"映射对。Key可以看成是Value 的索引,作为key的对象在集合中不可重复。

HashMap

HashMap通过Node<K,V>数组+链表(红黑树)实现的key-value数据结构,每个Node有自己的key,value

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 默认容量 16
    static final int MAXIMUM_CAPACITY = 1 << 30; // 最大容量 2^30
    static final float DEFAULT_LOAD_FACTOR = 0.75f; // 默认扩容因子0.75
    // 当链表元素个数到达8时,转化为红黑树
    static final int TREEIFY_THRESHOLD = 8;
    // 树中元素个数小于等于6时,由红黑树转位链表
    static final int UNTREEIFY_THRESHOLD = 6;
    static final int MIN_TREEIFY_CAPACITY = 64;
    transient Node<K,V>[] table; // Node数组 需要注意:table的大小始终是2^n
    transient int size; // 集合大小
    // 扩容阈值,当元素个数达到多少时进行扩容,threshold = capacity * loadFactor
    int threshold;
    // 内部类,Node结点,单向链表
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash; // hash值
        final K key;
        V value;
        Node<K,V> next; // next结点,单向链表
        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }
        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }
    }
    // 内部类,TreeNode结点,红黑树
    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
        TreeNode<K,V> parent;  // 父节点
        TreeNode<K,V> left;    // 左子节点
        TreeNode<K,V> right;   // 右子节点
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
        boolean red;           // 颜色:红或黑
        TreeNode(int hash, K key, V val, Node<K,V> next) {
            super(hash, key, val, next);
        }
    }
    // 构造方法
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +  initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +  loadFactor);
        this.loadFactor = loadFactor;
        // 计算扩容阈值
        this.threshold = tableSizeFor(initialCapacity);
    } 
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    } 
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    } 
    public HashMap(Map<? extends K, ? extends V> m) {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }
}

put方法

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
    Node<K,V>[] tab; Node<K,V> p; int n, i;
    if ((tab = table) == null || (n = tab.length) == 0)
        n = (tab = resize()).length;// 如果table为空,resize()
    // (n - 1) & hash n为2的幂 n-1 转为2进制 - 15 ——> 1111 所有位都是1    
    // (n - 1) & hash 与运算,结果肯定在 0 - n-1 之间
    if ((p = tab[i = (n - 1) & hash]) == null) // 如果table数组下标位置没有元素
        tab[i] = newNode(hash, key, value, null); // 创建新元素
    else {
        Node<K,V> e; K k;
        if (p.hash == hash &&  // hash 相等,key也相等
            ((k = p.key) == key || (key != null && key.equals(k))))
            e = p;
        else if (p instanceof TreeNode) // 如果当前节点为红黑树,红黑树插入结点
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        else { // 链表
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) { // 遍历链表,
                    p.next = newNode(hash, key, value, null);// 尾部新增一个结点
                    if (binCount >= TREEIFY_THRESHOLD - 1) // 如果链表长度过长(当前没有执行 ++binCount )
                        treeifyBin(tab, hash); // 将当前链表转为红黑树
                    break;
                }
                // 找到 hash 相等,key也相等 的 节点 e break
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        // 找到 hash 相等,key也相等的节点 e
        if (e != null) {  
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value; // value重新赋值
            afterNodeAccess(e); // 空方法,LinkedHashMap中重写
            return oldValue;
        }
    }
    ++modCount;
    if (++size > threshold) // 当前size大于扩容阈值
        resize(); // 扩容
    afterNodeInsertion(evict); // 空方法,LinkedHashMap中重写
    return null;
} 

hash算法

// 取hash
static final int hash(Object key) {
    int h; // 高16位和低16位做异或运算,保证hash的离散性
    // 设想一下,如果n很小,假设为16的话,那么n-1即为15(0000 0000 0000 0000 0000 0000 0000 1111),
    // 这样的值如果跟hashCode()直接做与操作,实际上只使用了哈希值的后4位。如果当哈希值的高位变化很大,低位变化很小,
    // 这样很容易造成碰撞,所以把高低位都参与到计算中,从而解决了这个问题,而且也不会有太大的开销。
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
// 根据hash计算tab数组的下标 i = (n - 1) & hash
// 由于n始终是2的整数次方,n-1的二进制都为1 如15(1111) ;一个数和15做与运算,可以去掉15的最高位之前的数字
// 这样[与运算]可以实现和取模相同的效果
if ((p = tab[i = (n - 1) & hash]) == null)  
    tab[i] = newNode(hash, key, value, null); 

扩容

// 对table进行初始化或者扩容。
// 如果table为null,则对table进行初始化
// 如果对table扩容,因为每次扩容都是翻倍,与原来计算(n-1)&hash的结果相比,
// 节点要么就在原来的位置,要么就被分配到“原位置+旧容量”这个位置。
final Node<K,V>[] resize() {
    // 新建oldTab数组保存扩容前的数组table
    Node<K,V>[] oldTab = table;
    int oldCap = (oldTab == null) ? 0 : oldTab.length; // 当前数组大小
    int oldThr = threshold; // 扩容阈值
    int newCap, newThr = 0;
    if (oldCap > 0) {
        if (oldCap >= MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return oldTab;
        }
        else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && // newCap = 原容量翻倍
                 oldCap >= DEFAULT_INITIAL_CAPACITY)
            newThr = oldThr << 1; // 扩容阈值翻倍
    }
    else if (oldThr > 0) // oldCap == 0 && oldThr > 0 说明计算了扩容阈值,但是table没有初始化
        newCap = oldThr;
    else {               // 默认构造
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
    if (newThr == 0) {
        float ft = (float)newCap * loadFactor;
        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                  (int)ft : Integer.MAX_VALUE);
    }
    threshold = newThr; // 扩容阈值重新赋值
    @SuppressWarnings({"rawtypes","unchecked"})
    Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
    table = newTab;
    // 如果旧table不为空,将旧table中的元素复制到新的table中
    if (oldTab != null) {
        // 遍历旧哈希表,将旧哈希表中的数据复制到新的哈希表中
        for (int j = 0; j < oldCap; ++j) {
            Node<K,V> e;
            if ((e = oldTab[j]) != null) {
                oldTab[j] = null;
                if (e.next == null)
                    // 重新取hash位置
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)
                    // 将树中的node分离
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                else { // 链表
                    // 因为每次扩容都是翻倍,与原来计算(n-1)&hash的结果相比,
                    // 节点要么就在原来的位置,要么就被分配到“原位置+旧容量”这个位置。
                    // 所以构建两个链表(loTail和hiTail),loTail在原来的位置,hiTail在“原位置+旧容量”的位置
                    Node<K,V> loHead = null, loTail = null;
                    Node<K,V> hiHead = null, hiTail = null;
                    Node<K,V> next;
                    do {
                        next = e.next;
                        if ((e.hash & oldCap) == 0) {
                            if (loTail == null)
                                loHead = e;
                            else
                                loTail.next = e;
                            loTail = e;
                        }
                        else {
                            if (hiTail == null)
                                hiHead = e;
                            else
                                hiTail.next = e;
                            hiTail = e;
                        }
                    } while ((e = next) != null);
                    if (loTail != null) {
                        loTail.next = null;
                        newTab[j] = loHead; // 低位链表
                    }
                    if (hiTail != null) {
                        hiTail.next = null;
                        newTab[j + oldCap] = hiHead;// 高位链表
                    }
                }
            }
        }
    }
    return newTab;
}

get方法

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
} 
final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (first = tab[(n - 1) & hash]) != null) {// 根据hash找到数组的下标
        if (first.hash == hash && // first结点 hash 相等,key也相等 
            ((k = first.key) == key || (key != null && key.equals(k))))
            return first;
        if ((e = first.next) != null) { // 存在后续结点
            if (first instanceof TreeNode) // 如果是红黑树,通过红黑树查询
                return ((TreeNode<K,V>)first).getTreeNode(hash, key);
            do {
                // 遍历链表 找到 hash 相等,key也相等 的结点
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}

remove方法

public V remove(Object key) {
    Node<K,V> e;
    return (e = removeNode(hash(key), key, null, false, true)) == null ?
        null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) {
    Node<K,V>[] tab; Node<K,V> p; int n, index;
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (p = tab[index = (n - 1) & hash]) != null) { // 根据hash找到数组的下标
        Node<K,V> node = null, e; K k; V v;
        if (p.hash == hash && // 第一个结点 hash 相等,key也相等 
            ((k = p.key) == key || (key != null && key.equals(k))))
            node = p;
        else if ((e = p.next) != null) { // 存在后续结点
            if (p instanceof TreeNode) // 如果是红黑树,通过红黑树查询
                node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
            else {
                do {
                    // 遍历链表 找到 hash 相等,key也相等 的结点
                    if (e.hash == hash &&
                        ((k = e.key) == key ||
                         (key != null && key.equals(k)))) {
                        node = e;
                        break;
                    }
                    p = e;
                } while ((e = e.next) != null);
            }
        }
        // node 不为null
        if (node != null && (!matchValue || (v = node.value) == value ||
                             (value != null && value.equals(v)))) {
            if (node instanceof TreeNode) // 移除红黑树中的结点
                ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
            else if (node == p)
                tab[index] = node.next;
            else
                p.next = node.next;
            ++modCount;
            --size;
            afterNodeRemoval(node); // 空方法,LinkedHashMap中重写
            return node;
        }
    }
    return null;
} 

Hashtable

Hashtable使用synchronized 来进行同步

public class Hashtable<K,V> extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {
    private transient Entry<?,?>[] table;  // table
    private transient int count;    
    private int threshold;   // 扩容阈值
    private float loadFactor;// 扩容因子
    private static class Entry<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Entry<K,V> next; // next结点,单向链表
        protected Entry(int hash, K key, V value, Entry<K,V> next) {
            this.hash = hash;
            this.key =  key;
            this.value = value;
            this.next = next;
        }
    }
    // 构造方法
    public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor); 
        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry<?,?>[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    } 
    public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }
 	// 默认构造方法 数组长度11,扩容因子0.75
    public Hashtable() {
        this(11, 0.75f);
    } 
    public Hashtable(Map<? extends K, ? extends V> t) {
        this(Math.max(2*t.size(), 11), 0.75f);
        putAll(t);
    }
}

put方法

使用synchronized同步锁,保证线程安全

public synchronized V put(K key, V value) { 
    if (value == null) { // 不能插入null
        throw new NullPointerException();
    } 
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;// 通过取模找到hash下标
    Entry<K,V> entry = (Entry<K,V>)tab[index];
    for(; entry != null ; entry = entry.next) { // 遍历链表
        if ((entry.hash == hash) && entry.key.equals(key)) {
            V old = entry.value;
            entry.value = value; // value重新赋值
            return old;
        }
    }
    addEntry(hash, key, value, index);
    return null;
}
// 添加元素
private void addEntry(int hash, K key, V value, int index) {
    modCount++; 
    Entry<?,?> tab[] = table;
    if (count >= threshold) { 
        rehash(); // 扩容并重新hash
        tab = table;
        hash = key.hashCode();
        index = (hash & 0x7FFFFFFF) % tab.length;
    } 
    // Creates the new entry.
    @SuppressWarnings("unchecked")
    Entry<K,V> e = (Entry<K,V>) tab[index];
    // 把就结点e 作为新结点 的next结点新建一个Entry结点;相当于把新结点插入到链表的最前端
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
}

hash算法

// 取hash
int hash = key.hashCode();
// 通过取模计算当前key在数组中的下标index
int index = (hash & 0x7FFFFFFF) % tab.length; 

扩容

protected void rehash() {
    int oldCapacity = table.length;
    Entry<?,?>[] oldMap = table; 
    // 旧容量翻倍+1
    int newCapacity = (oldCapacity << 1) + 1;
    if (newCapacity - MAX_ARRAY_SIZE > 0) {
        if (oldCapacity == MAX_ARRAY_SIZE)
            // Keep running with MAX_ARRAY_SIZE buckets
            return;
        newCapacity = MAX_ARRAY_SIZE;
    }
    Entry<?,?>[] newMap = new Entry<?,?>[newCapacity]; 
    modCount++;
    threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    table = newMap;
	// 遍历所有元素,重新hash放入数组
    for (int i = oldCapacity ; i-- > 0 ;) {
        for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
            Entry<K,V> e = old;
            old = old.next; 
            int index = (e.hash & 0x7FFFFFFF) % newCapacity; // 重新计算下标
            e.next = (Entry<K,V>)newMap[index];
            newMap[index] = e;
        }
    }
} 

get方法

使用synchronized同步锁,保证线程安全

public synchronized V get(Object key) {
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length; // 计算下标
    for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            return (V)e.value;
        }
    }
    return null;
}

remove方法

使用synchronized同步锁,保证线程安全

public synchronized V remove(Object key) {
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K,V> e = (Entry<K,V>)tab[index];
    for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            modCount++;
            if (prev != null) { // 删除链表结点
                prev.next = e.next;
            } else {
                tab[index] = e.next;
            }
            count--;
            V oldValue = e.value;
            e.value = null;
            return oldValue;
        }
    }
    return null;
}

HashMap和Hashtable

Hashtable和HashMap都是key-value的映射集合

  • 数据结构:Hashtable与 HashMap都采用数组+链表的结构存储数据,HashMap在链表长度超过8时会将链表转换为红黑树,提高查询效率
  • 线程安全:Hashtable的方法都有synchronized修饰,所以Hashtable线程安全的,HashMap是非线程安全的
  • 性能:由于Hashtable有synchronized修饰,HashMap 性能较好
  • Hashtable 是不允许key或value为 null 的,HashMap 的key和value则都可以为 null
  • Hashtable的默认容量是11,HashMap的默认容量是16,扩容因子都是0.75,Hashtable扩容后的容量2n+1,HashMap是2n;
  • Hashtable的hash算法是求余法,HashMap是与运算(容量必须是2的整数次幂)

TreeMap

TreeMap实际是一棵红黑树

public class TreeMap<K,V> extends AbstractMap<K,V>
    implements NavigableMap<K,V>, Cloneable, java.io.Serializable {
    private transient Entry<K,V> root; // 根结点
    private transient int size = 0; // 集合大小
    // 红黑树结点
    static final class Entry<K,V> implements Map.Entry<K,V> {
        K key;
        V value;
        Entry<K,V> left; // 左子结点
        Entry<K,V> right; // 右子结点
        Entry<K,V> parent; // 父结点
        boolean color = BLACK; // 红色还是黑树
        Entry(K key, V value, Entry<K,V> parent) {
            this.key = key;
            this.value = value;
            this.parent = parent;
        }
    }
    // 构造方法
    public TreeMap() {
        comparator = null;
    } 
    public TreeMap(Comparator<? super K> comparator) {
        this.comparator = comparator;
    } 
    public TreeMap(Map<? extends K, ? extends V> m) {
        comparator = null;
        putAll(m);
    } 
    public TreeMap(SortedMap<K, ? extends V> m) {
        comparator = m.comparator();
        try {
            buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
        } catch (java.io.IOException cannotHappen) {
        } catch (ClassNotFoundException cannotHappen) {
        }
    }
}

put方法

使用synchronized同步锁,保证线程安全

public V put(K key, V value) {
    Entry<K,V> t = root;
    if (t == null) { // 如果root结点为null
        compare(key, key); 
        root = new Entry<>(key, value, null); // 设置当前插入的结点为root
        size = 1;
        modCount++;
        return null;
    }
    int cmp;
    Entry<K,V> parent; 
    Comparator<? super K> cpr = comparator;
    if (cpr != null) { // Comparator 参数不为空
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);
            if (cmp < 0) // 小于0,左子结点
                t = t.left;
            else if (cmp > 0) // 大于0,右子结点
                t = t.right;
            else // 等于0,说明当前结点key和put的key一样
                return t.setValue(value);
        } while (t != null);
    } else {
        if (key == null)
            throw new NullPointerException(); 
        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); // 修正红黑树
    size++;
    modCount++;
    return null;
}

get方法

public V get(Object key) {
    Entry<K,V> p = getEntry(key);
    return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) { 
    if (comparator != null)
        return getEntryUsingComparator(key);
    if (key == null)
        throw new NullPointerException();
    @SuppressWarnings("unchecked")
    Comparable<? super K> k = (Comparable<? super K>) key;
    Entry<K,V> p = root;
    // 二叉查找树的查找方法
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0) // <0,向左查询
            p = p.left;
        else if (cmp > 0)// >0,向右查询
            p = p.right;
        else
            return p;
    }
    return null;
}

remove方法

public V remove(Object key) {
    Entry<K,V> p = getEntry(key); // 找到结点
    if (p == null)
        return null;
    V oldValue = p.value;
    deleteEntry(p);
    return oldValue;
}
private void deleteEntry(Entry<K,V> p) {
    modCount++;
    size--;

    // If strictly internal, copy successor's element to p and then make p
    // point to successor.
    if (p.left != null && p.right != null) {
        Entry<K,V> s = successor(p);
        p.key = s.key;
        p.value = s.value;
        p = s;
    } // p has 2 children

    // Start fixup at replacement node, if it exists.
    Entry<K,V> replacement = (p.left != null ? p.left : p.right);

    if (replacement != null) {
        // Link replacement to parent
        replacement.parent = p.parent;
        if (p.parent == null)
            root = replacement;
        else if (p == p.parent.left)
            p.parent.left  = replacement;
        else
            p.parent.right = replacement;

        // Null out links so they are OK to use by fixAfterDeletion.
        p.left = p.right = p.parent = null;

        // Fix replacement
        if (p.color == BLACK)
            fixAfterDeletion(replacement);
    } else if (p.parent == null) { // return if we are the only node.
        root = null;
    } else { //  No children. Use self as phantom replacement and unlink.
        if (p.color == BLACK)
            fixAfterDeletion(p);

        if (p.parent != null) {
            if (p == p.parent.left)
                p.parent.left = null;
            else if (p == p.parent.right)
                p.parent.right = null;
            p.parent = null;
        }
    }
}

参考