HashSet理解(三)add方法(jdk1.7及以前)是如何插值的

HashSet理解(一)java集合
HashSet理解(二)怎么做到值不重复
HashSet理解(三)add方法(jdk1.7及以前)是如何插值的
HashSet理解(四)为什么jdk1.7中的头插法会形成环和死循环?

& (按位与)运算的结果

 &运算符,具体怎么算的,这篇:java运算符手把手教你计算。按位与有个特点,算式x&y中,当y=2^n-1时,例如3,7,15等,其作用类似于x%(y+1)。如下所示,x&15的结果,类似于x%16

    public static void main(String[] args) {
        System.out.println(15&15);
        System.out.println(16&15);
        System.out.println(17&15);
        System.out.println(18&15);
        System.out.println(19&15);
        System.out.println(20&15);
        System.out.println(21&15);
        System.out.println(22&15);
        System.out.println(23&15);
    }

运行结果:

indexFor()本质是求余运算(%)
    static int indexFor(int h, int length) {
        return h & (length-1);
    }

length初始值是16,length-1就是15。

jdk1.7及以前,插值其实是头插法

put方法具体:

public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

看看addEntry()方法的四个参数:

  • hash是hash(key.hashCode());的返回值,并不是key.hashCode()直接传入。
  • key,value都put方法的入参
  • i 是hash值对应的数组下标

继续看,这四个参数怎么处理的:

//jdk 1.6
void addEntry(int hash, K key, V value, int bucketIndex) {
    Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
        if (size++ >= threshold)
            resize(2 * table.length);
    }

可以看出,在jdk1.6中,HashMap是先插入,再扩容。

 e是单链表的头结点,new Entry<K,V>(hash, key, value, e);是新的头结点,看看,Entry的构造函数里面干了啥?

static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;//对下一个节点的引用(看到链表的内容,结合定义的Entry数组,是不是想到了哈希表的拉链法实现?!)
        final int hash;//哈希值

        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n; //原头结点放在新头结点的尾部
            key = k;
            hash = h;
        }
        ...
    }

hash值,key,value直接保存在entry里面,然后让next等于原来的头结点。这就是整个完整的头插法。核心代码:

    Entry<K,V> e = table[bucketIndex]; 
    table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
    //类似于 Entry e= head; 
    // head= newEntyr;
    // head.next=e;

更多详细的源码分析,参考:HashMap源码分析.

为什么没见处理哈希冲突?

哈希冲突简单的讲,就是两个key的hash值相同。详细描述参考:数据结构与算法:hash冲突解决。在上面的put方法中,for循环的判断条件:e!=null就说明相同的哈希值,已经有entry插入了,已经哈希冲突了。

如何处理hash冲突?
  • 拉链法(链地址法)
  • 开放定址法
  • 再hash法
    HashMap处理hash冲突的方式,就是拉链法,即当hash值相同时,直接将entry插入到链表头部。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值