HashMap源码讲解一 hash算法

1 什么是hash算法

  来自百度百科

  Hash算法可以将一个数据转换为一个标志,这个标志和源数据的每一个字节都有十分紧密的关系。Hash算法还具有一个特点,就是很难找到逆向规律

   个人批语

通过hash算法可以把一个数据转换为一个值,这个值暂用很小的空间,但是这个转换是不可逆的

2 Java中hash算法的应用

  

 java 的 根对象 Object 存在 hashCode方法,是native修饰

public native int hashCode();

个人批语

   自己新建的对象需要重新这个方法,不然得到的hascode值相同

    得到的hash值都是int类型

3 hash算法在 HashMap中的应用

        HashMap 是Key Value的存储形式,首先是数组加链表加红黑树的数据结构,首先说数组

 /* ---------------- Fields -------------- */
 
    /**
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node[] table;

    

   问题反思

           数组的存储是根据索引进行的,那么如何确定一个key应该在数组何处索引位置

  Key如何确定索引位置   

       1  首先把Key取hash,得到一个int类型的hashcode      

 /**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    2 计算key对应的下标值    

    

i = (n - 1) & hash

从源码中可以借鉴的思想

     为key取hash值然后存储在数组中的思想,很多中间件也有类似的实现,比如

   Redis Cluster 槽位

   Kafka 解决消费顺序行

  

你可能感兴趣的:(Java,hashmap)