HashMap的简单源码分析
来源:互联网 发布:java常用包及类 编辑:程序博客网 时间:2024/06/06 14:07
关于HashMap
- 关于HashMap
- 容量
- 加载因子
- Node
- Node数组
- 关于HashMap
1.容量
2.加载因子
1和2的决定了方法resize()
并且加载因子会决定空间的利用率和hash冲突的几率,
默认的加载因子是0.75
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 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) { loTail.next = null; newTab[j] = loHead; } if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }
3.Node
static class Node<K,V> implements Map.Entry<K,V> { final int hash;// 通过key对象产生的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; } }
显而易见的链表
4.Node数组
put
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; // 判断数组是否为空 if ((tab = table) == null || (n = tab.length) == 0) // resize() 对数组进行初始化或者扩容 这个地方是初始化 n = (tab = resize()).length; // 在数组中定位 hash 通过key对象产生的 具体参考-hash函数 如果该位置没有值 if ((p = tab[i = (n - 1) & hash]) == null) // 为该位置赋值 tab[i] = newNode(hash, key, value, null); else { // 当前位置有值 Node<K,V> e; K k; // 判断当前位置上面的key和当前存放的key是否一致 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; // key一致 进行值的覆盖 else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { // 这个地方体现出来了链表 p.next = newNode(hash, key, value, null); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st // 这个地方是一个优化的操作 如果连败哦的长度大于等于一个指定的值 则将链表改为树的结构 treeifyBin(tab, hash); break; } // 判断是否key一致 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } // 如果当前key已经存在进行值的覆盖 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) // 判断是否需要进行扩容 resize(); afterNodeInsertion(evict); return null; }
get
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; }
final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // 关于first的赋值,与put相对应。找到每个数组位置对应的链表的第一个元素 if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode<K,V>)first).getTreeNode(hash, key); do { // 遍历链表 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; }
关于对链表的优化通过树实现请自行参考源码分析
阅读全文
0 0
- HashMap的简单源码分析
- HashMap的源码分析
- HashMap的源码分析
- HashMap 的源码分析
- hashmap的源码分析
- HashMap的containskey源码分析
- 关于HashMap的源码分析
- 【源码分析】HashMap的数据结构
- 从HashMap到LruCache的源码分析
- 从HashMap到LruCache的源码分析
- 史上最详细的HashMap详解--源码分析
- java源码分析--HashMap的工作原理
- java HashMap的插入操作源码分析
- JDK1.8的HashMap源码分析
- HashMap源码分析,为什么是无序的?
- 源码分析---HashMap的底层结构
- HashMap的底层原理及源码分析
- 源码分析:HashMap
- 浅谈IT领域的发展规律及成功发展案例
- jwhois 命令 --英语版啊!!!
- UICollectView自定义Layout布局
- java创建对象
- [BFS] POJ 3984
- HashMap的简单源码分析
- Centos配置yum源
- redis详解-(12)TwemProxy
- 支持向量机(一)
- 周中训练笔记16
- Java Jedis远程连接redis异常解决思路,异常描述:Could not get a resource from the pool && connect timed out
- XPath定位——基于Google Chrome和Firefox
- Navi.Soft31.WebMVC框架(含示例地址)
- python大作战之不可变序列(元组tuple)