问题 :
- HashMap 容量大小 (capacity)为什么为 2n
- HashMap 是线程安全的吗,为什么
- HashMap 既然有hash进行排位还需要equals()作用是什么
文章部分图片和代码来自参考资料,属于半原创
概述
HashMap 属于字典类,以键值对的方式存储值, 通过计算 hash 值,把key 放在特定的位置,当计算得到的键相同将会以链表的形式在冲突点链接, java 8 中当链表长度达到一定数量,该链表会形成一个红黑树,加快查找。所以java8 的HashMap内部的数据结构就成了 “数组+链表+红黑树” (图片出处见参考资料)
源码解析
主要我们关注的的是get 和 put 方法
put 方法
public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } // 第三个参数 onlyIfAbsent 如果是 true,那么只有在不存在该 key 时才会进行 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; // 第一次 put 值的时候,会触发下面的 resize(),初始化数组长度 // 第一次 resize 和后续的扩容有些不一样,因为这次是数组从 null 初始化到默认的 16 或自定义的初始容量 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // 找到具体的数组下标,如果此位置没有值,那么直接初始化一下 Node 并放置在这个位置就可以了 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else {// 数组该位置有数据 Node<K,V> e; K k; // 首先,判断该位置的第一个数据和我们要插入的数据,key 是不是"相等"或是这个地方还没插入数值 //,如果是,取出这个节点 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; // 如果该节点是代表红黑树的节点,调用红黑树的插值方法,本文不展开说红黑树 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); // TREEIFY_THRESHOLD 为 8,所以,如果新插入的值是链表中的第 8 个 // 会触发下面的 treeifyBin,也就是将链表转换为红黑树 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); break; } // 如果在该链表中找到了"相等"的 key(== 或 equals) if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) // 此时 break,那么 e 为链表中[与要插入的新值的 key "相等"]的 node break; p = e; } } // e!=null 说明存在旧值的key与要插入的key"相等" // 对于我们分析的put操作,下面这个 if 其实就是进行 "值覆盖",然后返回旧值 if (e != null) { V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; // 如果 HashMap 由于新插入这个值导致 size 已经超过了阈值,需要进行扩容 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; } 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) // 对应使用 new HashMap(int initialCapacity) 初始化后,第一次 put 的时候 newCap = oldThr; else {// 对应使用 new HashMap() 初始化后,第一次 put 的时候,初始化数组(容器) 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; // 用新的数组大小初始化新的数组 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; table = newTab; // 如果是初始化数组,到这里就结束了,返回 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 { // 这块是处理链表的情况, // 需要将此链表拆成两个链表,放到新的数组中,并且保留原来的先后顺序 // loHead、loTail 对应一条链表,hiHead、hiTail 对应另一条链表,代码还是比较简单的 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; // 第二条链表的新的位置是 j + oldCap,这个很好理解 newTab[j + oldCap] = hiHead; } } } } } return newTab; }
扩容时链表迁移可能有点难理解,可以看这里 :
而有关红黑树的操作可以阅读这篇文章
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; 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; }
总结
HashMap 容量大小 (capacity)为什么为 2n
设计到hash值的计算,hash 最简单的方法就是取余,取余的操作可以用移位来代替(移位相比取余操作提高速度和性能),这关系到一个数学,详情看 hash计算。
HashMap 是线程安全的吗,为什么
不安全。因为在扩容的时候迁移数据的操作并非原子操作,同时迭代的时候会产生 fail-fast ,java7 的hashmap 会产生环形锁。
HashMap 既然有hash进行排位还需要equals()作用是什么
思考这个问题需要知道 hashCode 和 equals 两个方法的作用是什么?
hashCode 方法和equal都是object 方法,hashcode返回的是对象的hash值,一般返回的是对象的引用地址。equals 这是逻辑上判断两个对象是不是相同的,例如在HashMap 中假如某个位置已经有一个 n1<“s”,3> , 现在要更新的传进来的是<”s”,5>,那么这个”s“在逻辑上是相同的东西吗?很明显这两个对象是在逻辑应该认同是相同的。通常有以下原则 :
- 两对象equals相等(逻辑上认为是相同的),那么hashCode 也必然要是相等
- equals 不相等,hashcode有可能一样也有可能不一样。
- 为了保证第一条原则,就要求我们在要是重写了equals 方法,那么就要重写 hashCode方法。
综上,使用hash进行排位后还需使用equals的原因是: hash相等和equals 相等后可以从逻辑上确定这两者是相同的东西,例如String 重写了 hashCode 和 equals 方法。
下面看一下这几个类的 equals 方法 和 hashcode 方法
//Object 中的hashCode 方法 和 equals 方法 //可以看到hashcode 的实现不由 java提供,源码注解中也有说明 public native int hashCode(); //比较一下引用地址 public boolean equals(Object obj) { return (this == obj); } //String 重写了这两个方法 public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; } public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; } //hashMap中定义的节点(内部类 Node)也重写了这两个方法 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 final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } //Objects.equals 的方法 public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); }
HashSet
说到 HashMap就有必要说一下 HashSet ,为什么呢?
private transient HashMap<E,Object> map; public HashSet() { map = new HashMap<>(); } public boolean add(E e) { return map.put(e, PRESENT)==null; } public boolean remove(Object o) { return map.remove(o)==PRESENT; }
对于HashSet 我们需要知道 :
- HashSet 实际上是调用 HashMap 的方法
- 它不允许集合中出现重复元素
- HashSet 是线程不安全的