HashMap底层实现

链表定义

     Hashmap中定义了一个Node结构,很明显可以看出这是一个链表的节点定义。键值对key和value以及key的hash值都储存在这个节点中

static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
}

插入

     HashMap的插入方法在key值已存在的时候是直接替换掉value值,若key值不存在才执行插入,插入方法定义如下

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;
        /*table是Hashmap中维护数据的结构,它是由链表构成的数组,第一条if
        判定如果该表不存在或者为空,那么就用resize方法初始化生成一个链表
        数组,resize方法在链表为空的时候创建一个数组,或者在需要的时候将
        表的大小直接翻倍*/
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        /*第二条if语句判定如果hash值所对应的数组位置的元素为空,那么就以
        插入值为链表头节点创建一个链表,这里采用的是表的最大下标与hash值
        求与,效果与mod一样*/
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        /*一般情况*/
        else {
            Node<K,V> e; K k;
            /*如果插入值的key刚好与头节点的key值相同,那么就替换表头的value值*/
            if (p.hash == hash &&
                ((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);
            /*链表中插入,从首部遍历到尾部,若找到相同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) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

可以看出Hashmap插入的步骤如下:

  1. 首先判断Hash表是否存在,若不存在则创建一个;
  2. 再通过hash值找到对应的位置,如果该位置为空,那么就以插入值为头节点创建一个新链表;
  3. 判断表头的key值是否与插入key值相同,若相同则替换value值,然后判断是否是红黑树结构,若是,则调用红黑树的插入方法,若不是,则可判断它是链表,接下来从头遍历到尾,若找到与插入key值相同的节点,则替换value值,否则在末尾插入一个新的节点,若链表长度达到所定的阈值(默认为8),则将其转换成一颗红黑树。

取出

     取出的代码如下:

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) {
            if (first.hash == hash && // always check first node
                ((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 {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

查找的步骤很简单,先判断表是否存在或者为空,再判断头节点是否是所要查找的节点,若不是,则根据根据是红黑树还是链表来进行查找。

删除

     删除的代码如下:

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) {
            Node<K,V> node = null, e; K k; V v;
            /*若头节点是所要移除的节点。则使node = p*/
            if (p.hash == hash &&
                ((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);
                /*结构是链表,且操作节点不是头节点,那么用p维护node的前驱
                节点,依次向后遍历,直到找到所要删除的节点node*/
                else {
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            node = e;
                            break;
                        }
                        p = e;
                    } while ((e = e.next) != 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;
                /*p维护前驱节点,直接删除就行了*/
                else
                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

删除的步骤如下:

  1. 首先判断表是否存在,否则返回null;
  2. 再判断删除节点是头节点,还是红黑树中的节点,又或者是链表中间位置的节点,根据3种情况分别进行操作,其中红黑树删除节点后在元素少于阈值的时候会转换成链表结构。

如有错误,请进行指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值