hashMap原理解析

        我们都知道,在hashMap中不论是计算key的hashcode还是为了使得hashMap中元素分布更均匀的场景,hash算法的能力都不可低估。今天,小编就和大家一起来分享在hashMap中hash算法的研究:

        在述说原理之前,我们先来看一个hash算法在生活中的例子:

        在某农场A,主人养了50头小猪,由于在生长期,主人需要定期地根据每头小猪的体重调整它们的“宿舍”,假设现在有10个猪舍(每个猪舍都有体重的范围),那么主人需要根据每头小猪的体重来分猪舍。这样,每头小猪都有了自己的住处,而且这种方法相当于主人对当前这50头小猪进行了分类,甚至是排序。当某一天,主人需要找某一头时只需要知道它的体重是多少,就可以很快的定位到它的位置,在确定的猪舍中进行查找会很快捷并且方便,就不需要对50头小猪都进行排查。

       其实这就是一个hash算法的实现过程,在hashmap中,当我们put某一个元素时,hashMap内部会做几层操作:计算当前key的hashCode --> 获取该key在数组中的index --> 把key放置到确定好的数组 index对应的链表头部:

/**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        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;
    }
      其中涉及到求hashCode,根据code和数组长度-1 计算出的index值和添加当前元素到hashmap 中三步:

/**
     * Retrieve object hash code and applies a supplemental hash function to the
     * result hash, which defends against poor quality hash functions.  This is
     * critical because HashMap uses power-of-two length hash tables, that
     * otherwise encounter collisions for hashCodes that do not differ
     * in lower bits. Note: Null keys always map to hash 0, thus index 0.
     */
    final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

/**
     * Returns index for hash code h.
     */
    static int indexFor(int h, int length) {
        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
        return h & (length-1);
    }    

/**
     * Adds a new entry with the specified key, value and hash code to
     * the specified bucket.  It is the responsibility of this
     * method to resize the table if appropriate.
     *
     * Subclass overrides this to alter the behavior of put method.
     */
    void addEntry(int hash, K key, V value, int bucketIndex) {
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }

        createEntry(hash, key, value, bucketIndex);
    }       

      上述四段代码就是把每一只称过体重的小猪按照体重的范围,分配猪舍的过程。这样,我们可以保证每只小猪都已经有了很好的归属,而在所有的猪舍中由于体重分布不一定均匀,所有可能会出现某猪舍中有10头小猪,而某猪舍中只有两头,而这种场景在hashMap中的体现就是链表,如果有key相同的也就是hashMap中的碰撞现象,就将当前key插入到链表的头部,这样久而久之就形成了一个链表。也就是hashMap的数据结构:数组+链表!

     

        

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值