分享

HashMap底层原理实现源码探索(碰撞问题如何解决)

 vnxy001 2019-02-28

同样,HashMap是java面试中经常问到的一个知识点,基本都是和HashTable以及ConcurrentHashMap一起被问及,今天主要是讲讲HashMap是如何解决碰撞问题的?那么问题来了,什么是碰撞问题?

这要先从HashMap底层的实现说起,进入它的源码类

  1. public class HashMap<K,V> extends AbstractMap<K,V>
  2. implements Map<K,V>, Cloneable, Serializable

看到这我们就明白了HashMap继承了AbstractMap抽象类,并且实现了三个接口

  1. public HashMap() {
  2. this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
  3. }

再然后看到默认构造方法中有一个DEFAULT_LOAD_FACTOR,称为负载因子,可以看下静态参数

  1. static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
  2. static final int MAXIMUM_CAPACITY = 1 << 30;
  3. static final float DEFAULT_LOAD_FACTOR = 0.75f;

负载因子默认为0.75,当键值对数量占总容量的值大于0.75时,就会扩容两倍,而默认容量是1<<4,,也就是16

HashMap底层呢是一个数组,每个数组的元素是一个链表,而每个链表的节点都是一个映射,也就是键值对Entry,每个链表就是我们常说的“桶”,put和get方法都是从“桶”中进行的,如下图

往“桶”中存储时,也就是put时,会调用hash方法,对key的哈希码重新计算

  1. public V put(K key, V value) {
  2. return putVal(hash(key), key, value, false, true);
  3. }
  4. static final int hash(Object key) {
  5. int h;
  6. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  7. }
  8. final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
  9. boolean evict) {
  10. Node<K,V>[] tab; Node<K,V> p; int n, i;
  11. if ((tab = table) == null || (n = tab.length) == 0)
  12. n = (tab = resize()).length;
  13. if ((p = tab[i = (n - 1) & hash]) == null)
  14. tab[i] = newNode(hash, key, value, null);
  15. else {
  16. Node<K,V> e; K k;
  17. if (p.hash == hash &&
  18. ((k = p.key) == key || (key != null && key.equals(k))))
  19. e = p;
  20. else if (p instanceof TreeNode)
  21. e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
  22. else {
  23. for (int binCount = 0; ; ++binCount) {
  24. if ((e = p.next) == null) {
  25. p.next = newNode(hash, key, value, null);
  26. if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
  27. treeifyBin(tab, hash);
  28. break;
  29. }
  30. if (e.hash == hash &&
  31. ((k = e.key) == key || (key != null && key.equals(k))))
  32. break;
  33. p = e;
  34. }
  35. }
  36. if (e != null) { // existing mapping for key
  37. V oldValue = e.value;
  38. if (!onlyIfAbsent || oldValue == null)
  39. e.value = value;
  40. afterNodeAccess(e);
  41. return oldValue;
  42. }
  43. }
  44. ++modCount;
  45. if (++size > threshold)
  46. resize();
  47. afterNodeInsertion(evict);
  48. return null;
  49. }

可以看到计算方法是将key的哈希码与哈希码无符号右移16位之后的结果进行异或,这样的好处是保证key的高低位都参与哈希码的计算,高速少开销可靠性强,值得一提的是java8还有一个优化点,就是“桶”中的数量大于8时,就会将链表转换为一个二叉树,红黑树,这样做的直接好处时查询性能由O(logn)优化为O(nlogn),要实现红黑树就是进行比较,这也是HashMap实现Comparable接口的一个重要原因

存储时由hash值来确定要放到哪个“桶”中,那么问题就出现了,如果两个key有相同的hash值,怎么进行存储,这就是碰撞问题。

解决方法是“拉链法,其实也就是将entry键值对用链表来存储,一旦hash值相同,就会调用equals方法,如果为true,就会替代当前的entry,举个例子,比如

  1. public class PasswdKey {
  2. static String name="name";
  3. static String address="address";
  4. public static void main(String[] args) {
  5. Map<String,Object> map=new HashMap<>();
  6. map.put(name, "maze");
  7. map.put(name, "freg");
  8. }
  9. }

第一次存储键name时,无相同的hash值,直接存到合适freg的数组索引的位置,接着又存了一个键为name的entry,在进行hash比较时由于键都为name,hash值相同,然后对链表的每一个元素进行equals方法(此时已确定要存储的数组索引位置,与第一次的索引位置相同),如果相同,就替换当前位置的键值对,此时就会将键为name的value更换为“freg”,其实这就是我们所说的覆盖

同样,get以及containsKey方法也是过hash值来找到value的

  1. final Node<K,V> getNode(int hash, Object key) {
  2. Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
  3. if ((tab = table) != null && (n = tab.length) > 0 &&
  4. (first = tab[(n - 1) & hash]) != null) {
  5. if (first.hash == hash && // always check first node
  6. ((k = first.key) == key || (key != null && key.equals(k))))
  7. return first;
  8. if ((e = first.next) != null) {
  9. if (first instanceof TreeNode)
  10. return ((TreeNode<K,V>)first).getTreeNode(hash, key);
  11. do {
  12. if (e.hash == hash &&
  13. ((k = e.key) == key || (key != null && key.equals(k))))
  14. return e;
  15. } while ((e = e.next) != null);
  16. }
  17. }
  18. return null;
  19. }

另外说一下,HashMap是支持键值对为null的,此时存在数组的第一个位置上,索引为0

最后希望大家记住,碰撞问题非常影响map的性能,尽管java8相对于7有了很大的优化

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多