HashTable 的rehash方法
来源:互联网 发布:已备案过期域名查询 编辑:程序博客网 时间:2024/06/06 16:32
hashTable是一个数组的链表,本身是一个 Entry[] 数组,里面的一个对象放的是一个链表的结构。如果一个HashTable中key的hashcode相同,那么它就放在同一个链表中。
回到rehash中,在扩展容器本身的容量时,每个对象(key,value)的位置也会相应的发生调整。
源码内容如下:
protected void rehash() { int oldCapacity = table.length; Entry[] oldMap = table; // overflow-conscious code int newCapacity = (oldCapacity << 1) + 1; if (newCapacity - MAX_ARRAY_SIZE > 0) { if (oldCapacity == MAX_ARRAY_SIZE) // Keep running with MAX_ARRAY_SIZE buckets return; newCapacity = MAX_ARRAY_SIZE; } Entry[] newMap = new Entry[newCapacity]; modCount++; threshold = (int)(newCapacity * loadFactor); table = newMap; for (int i = oldCapacity ; i-- > 0 ;) { for (Entryold = oldMap[i] ; old != null ; ) { Entry e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = newMap[index]; newMap[index] = e; } } }
其中关键点在于下面这个for循环中,它先去从后向前的遍历数组,然后第二层循环去遍历数组中每个对象链表,重新计算链表中每一个节点的位置,这样两层循环下来,各个节点的位置就调整好了,容器容量也扩展了。
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry<K,V> old = oldMap[i] ; old != null ; ) {
Entry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
阅读全文
0 0
- HashTable 的rehash方法
- redis中hashtable 的 rehash/ resizing 策略
- lua table 的rehash
- ConcurrentHashMap的rehash过程
- mysql的auto-rehash简介
- Hashtable的一些方法
- Hashtable的遍历方法
- 修改Hashtable的排序方法
- Hashtable数组创建的方法
- java hashtable的put方法
- java Hashtable的遍历方法
- Redis2.2.2源码学习——dict中的hashtable扩容和rehash
- Redis2.2.2源码学习——dict中的hashtable扩容和rehash
- Redis2.2.2源码学习——dict中的hashtable扩容和rehash
- Hashtable 类的实用方法 BuildGlossary方法
- 最好的遍历hashtable的方法
- 两种遍历Hashtable的方法
- 遍历HashTable的两种方法
- tongue读音解析实例
- 【CSS布局】定位详解(二)
- EntityFramework6中管理DbContext的正确方式(三)【三种DbContext用法】
- struts2中关于同一xml、同一package下namespace与name关系
- Swift 字典
- HashTable 的rehash方法
- 视频容器格式与编码格式简介
- Unity Shader:Waveform波形(2)-基本波形:正弦,三角,锯齿,直角以及其变种的实现方式
- idea 破解
- Android图片加载框架最全解析(一),Glide的基本用法
- Sql Server基本操作语句
- java中引用的用法
- 任务切换的精华思想
- Adreno GPU Profiler工具使用总结