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

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hughjin

动力动力动力动力动力动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值