HashMap 源码查看 (jdk1.8.0_31)

先从put方法开始看起。这里非常简单,调用了一个putVal的方法,然后将key调用了hash方法。

    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

 hash方法:

这里,先是判断了key的值是否为空,然后再次调用了key的hashCode方法。

变量h也就是key的hashCode值。这里将h和h右移16位后做异或运算。(这样做的原因不清楚,有可能是为了减少hash碰撞)

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

putVal方法:

这个方法比较复杂了,有两个参数先注明一下

onlyIfAbsent  对应值 fasle,evict 对应值  true。

方法开始声明了一个Node类型的数组 tab,这个应该就是HashMap对应的数组了。

然后又定义了一个Node类型的节点p,这个应该就是HashMap中的元素了。

(Node的源码在下面)

 

    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;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            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);
            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;
    }

 Node 的源码

这里看起来比较容易理解,Node是HashMap的一个内部类。实现了一个Map.Entry<K,V>的接口(也就是Map接口中的Entry接口)。

成员变量:

hash:这个就是我们之前hash方法计算的hash值了。

key 和 value 分别就是键 和 值了。

next:还是Node的定义,显现这里要实现链表啊。

方法:

hashCode: 这个方法居然不是返回的hash 值,而是key的hashCode 和 value的hashCode进行异或运算得到的。

equals:这个方法比较有意思的是调用了Objects.equals方法,这个方法里对第一个参数进行了非空判断。(这里是因为HashMap支持null值的原因吧?)

    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;
        }

        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }

        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值