美文网首页Java工程师知识树
Java基础-源码分析-hash 方法

Java基础-源码分析-hash 方法

作者: HughJin | 来源:发表于2021-01-08 08:24 被阅读0次

    Java工程师知识树 / Java基础


    不同Hash集合类中的hash方法分析

    在Map实现类中要找到某个元素,需要根据key的hash值来求得对应数组中的位置。如何计算这个位置就是hash算法。
    例如:HashMap的数据结构是数组和链表的结合,HashMap里面的元素位置需要尽量的分布均匀些,尽量使得每个位置上的元素数量只有一个,那么当用hash算法求得这个位置的时候,马上就可以知道对应位置的元素,而不用再去遍历链表。
    总结下HashMap在JDK1.7与1.8,HashTable在JDK1.8,ConcurrentHashMap在JDK1.8的hash算法,有兴趣的可以研究下为什么要在所在类中使用对应的hash算法。

    HashMap-JDK1.7

    //HashMap-JDK1.7 根据Key获取Entry
    Node<K,V>[] table;// 存储数据的Node数组,长度是2的幂
    
    // 1.根据key的hashCode重新计算hash值。  
    int hash = hash(key.hashCode());  
    // 2.搜索指定hash值在对应table中的索引。  
    int i = indexFor(hash, table.length); 
    // 3.根据索引获取Entry对象
    Entry<K,V> e = table[i]
    -------------------工具方法------------------------
    // 根据key的hashCode重新计算hash值
    static int hash(int h) {  
        h ^= (h >>> 20) ^ (h >>> 12);  
        return h ^ (h >>> 7) ^ (h >>> 4);  
    } 
    // 搜索指定hash值在对应table中的索引
    static int indexFor(int h, int length) {  
        return h & (length-1);  
    } 
    

    HashMap-JDK1.8

    // HashMap-JDK1.8 根据Key获取Entry
    Node<K,V>[] table;// 存储数据的Node数组,长度是2的幂
    // 1.根据key的hashCode重新计算hash值。 
    int hash = hash(key.hashCode());  
    // 2.搜索指定hash值在对应table中的索引。  
    int i = hash & (table.length-1);
    // 3.根据索引获取Entry对象
    Entry<K,V> e = table[i]
    
    -------------------工具方法------------------------    
    // 根据key的hashCode重新计算hash值。  
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    

    HashTable-JDK1.8

    // HashTable-JDK1.8 根据Key获取Entry
    private transient Entry<?,?>[] table;// 存储数据的Entry数组,长度是2的幂
        
    Entry<?,?> tab[] = table;
    // 1.获取key的HashCode值。 
    int hash = key.hashCode();
    // 2.搜索指定hash值在对应table中的索引
    int index = (hash & 0x7FFFFFFF) % tab.length;
    // 3.根据索引获取Entry对象
    Entry<K,V> e = (Entry<K,V>)tab[index];
    

    ConcurrentHashMap-JDK1.8

    // ConcurrentHashMap-JDK1.8 根据Key获取Entry
    transient volatile Node<K,V>[] table;// 存储数据的Node数组,长度是2的幂
    // 1.根据key的hashCode重新计算hash值。 
    int hash = spread(key.hashCode());
    // 2.搜索指定hash值在对应table中的索引。  
    int i = hash & (table.length-1);
    // 3.根据索引获取Node对象
    Node<K,V> e = tabAt(tab, i);
    
    
    -------------------工具方法------------------------
    //根据key的hashCode重新计算hash值
    static final int spread(int h) {
        return (h ^ (h >>> 16)) & HASH_BITS;
    }
    // 寻找指定数组tab在内存中i位置的数据
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
    }
    

    相关文章

      网友评论

        本文标题:Java基础-源码分析-hash 方法

        本文链接:https://www.haomeiwen.com/subject/vpiyoktx.html