模拟hashmap,运行扩容流程
来源:互联网 发布:怎样加入淘宝全球购 编辑:程序博客网 时间:2024/06/03 22:18
真实代码
final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; if (oldCap > 0) { if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // double threshold } else if (oldThr > 0) // initial capacity was placed in threshold newCap = oldThr; else { // zero initial threshold signifies using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } if (newThr == 0) { float ft = (float)newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); } threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; table = newTab; if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) != null) { oldTab[j] = null; if (e.next == null) newTab[e.hash & (newCap - 1)] = e; else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); else { // preserve order //以e.hash & oldCap是否为0进行数据分割,将一个桶的数据分散到两个桶里。 Node<K,V> loHead = null, loTail = null; Node<K,V> hiHead = null, hiTail = null; Node<K,V> next; do { next = e.next; if ((e.hash & oldCap) == 0) { if (loTail == null) loHead = e; else loTail.next = e; loTail = e; } else { if (hiTail == null) hiHead = e; else hiTail.next = e; hiTail = e; } } while ((e = next) != null); if (loTail != null) { //最后一个节点的next置空,防止死循环 loTail.next = null; //此部分存储数据位置不变 newTab[j] = loHead; } if (hiTail != null) { //最后一个节点的next置空,防止死循环 hiTail.next = null; //此部分存储数据是从原来的桶切分出来的 //在新table中的位置为原位置+T[原表长度] newTab[j + oldCap] = hiHead; } } } } } return newTab; }
模拟代码
package action;import java.util.Map;import java.util.Objects;public class TestClass<K,V>{ static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Node<K,V> next; Node(int hash, K key, V value, Node<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final K getKey() { return key; } public final V getValue() { return value; } public final String toString() { return key + "=" + value; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } } public static void main(String[] args) { Node<String, String> loHead0 = new Node<String, String>(3303, "10", "10", null); Node<String, String> loHead1 = new Node<String, String>(3303, "20", "20", loHead0); Node<String, String> loHead2 = new Node<String, String>(3303, "30", "30", loHead1); Node<String, String> loHead3 = new Node<String, String>(3303, "40", "40", loHead2); Node<String, String> loHead4 = new Node<String, String>(3303, "50", "50", loHead3); Node<String, String> loHead5 = new Node<String, String>(3303, "60", "60", loHead4); Node<String, String>[] oldTab = (Node<String, String>[])new Node[16]; oldTab[5] = loHead5; Node<String, String>[] newTab = (Node<String, String>[])new Node[32]; for (int j = 0; j < oldTab.length ; ++j) { Node<String, String> e; if ((e = oldTab[j]) != null) { Node<String, String> next; Node<String, String> loHead = null, loTail = null; Node<String, String> hiHead = null, hiTail = null; boolean flag = true; do { next = loHead5.next; if (flag) { if (loTail == null) loHead = loHead5; else loTail.next = loHead5; loTail = loHead5; flag = false; } else { if (hiTail == null) hiHead = loHead5; else hiTail.next = loHead5; hiTail = loHead5; flag = true; } } while ((loHead5 = next) != null); if (loTail != null) { loTail.next = null; newTab[j] = loHead; } if (hiTail != null) { hiTail.next = null; newTab[j + oldTab.length] = hiHead; } } } } }
扩容前:
扩容后:
阅读全文
0 0
- 模拟hashmap,运行扩容流程
- HashMap扩容
- HashMap扩容
- hashmap 扩容
- 合理应用HashMap扩容
- HashMap的扩容
- HashMap的扩容
- HashMap 的扩容机制
- HashMap哈希散列与扩容
- HashMap 扩容 加载因子
- Java HashMap的扩容
- HashMap扩容机制
- hashMap扩容机制
- HashMap扩容原理
- HashMap的内部结构及扩容
- HashMap和ArrayList如何扩容
- HashMap存储机制及扩容
- HashMap的容量与扩容
- Known Notation
- Ubuntu下Phabricator的Arcnist环境
- 【ros随手记】git工具版本更新
- 【AJAX】缓存问题和中文乱码
- HDU 1728逃离迷宫
- 模拟hashmap,运行扩容流程
- 第十三篇
- 逻辑回归
- 联想K50-T5/联想乐檬K3 Note组合键进入recovery模式的方法教程
- / Vijos / 题库 / 夜夜的NOIP之旅 P1964
- ubuntu下卸载python2和升级python3.5
- HDU 2204 Eddy's爱好(容斥)
- 模拟+灌水——填涂颜色
- 【AJAX】跨域问题