jdk 源码分析(1)java hashmap的结构

来源:互联网 发布:cg织梦 编辑:程序博客网 时间:2024/06/05 02:29
最近有人问我hashmap的底层是什么,冥冥中有点记忆,但是确实是忘了,所以今天把jdk的代码看来一下,后面几天将补几篇博客。

1)首先数据的存储定义了最小单元Node
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;    }
自定next ,hash, key和value

2)树结构:
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {    TreeNode<K,V> parent;  // red-black tree links    TreeNode<K,V> left;    TreeNode<K,V> right;    TreeNode<K,V> prev;    // needed to unlink next upon deletion    boolean red;    TreeNode(int hash, K key, V val, Node<K,V> next) {        super(hash, key, val, next);    }    /**     * Returns root of tree containing this node.     */    final TreeNode<K,V> root() {        for (TreeNode<K,V> r = this, p;;) {            if ((p = r.parent) == null)                return r;            r = p;        }    }

上面两者的区别是,当一个链表小于64时,用链表,链表只能顺序写,当数据大于64时可以用二叉树存储,这样就方便快速查找。

3)定义Node 集合:
transient Node<K,V>[] table;
这里的table 是放的相同不同hash的head。
比如hash ==1 或者hash ==2 的分别放在Node【1】和node[2]
至于相同的hash的其他值,可以通过next 来组成一个链表。

总而言之就是构成多个链表,每个链表的hash相同的Node。也就是key的hash相同,

3)put 方法,主要是找到链表,然后找到链表的末尾,将末尾的next 指向 Node 就可以了。
  1. final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
  2. boolean evict) {
  3. Node<K,V>[] tab; Node<K,V> p; int n, i;
  4. if ((tab = table) == null || (n = tab.length) == 0)
  5. n = (tab = resize()).length; //初始化
  6. if ((p = tab[i = (n - 1) & hash]) == null) //根据hash 找到链表,
  7. //如果链表为空,表示没有数据,放在第一个位置
  8. tab[i] = newNode(hash, key, value, null);  
  9. else {
  10. Node<K,V> e; K k;
  11. if (p.hash == hash &&
  12. ((k = p.key) == key || (key != null && key.equals(k))))
  13. e = p;
  14.             //按照树的结果来存储的。
  15. else if (p instanceof TreeNode)
  16. e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
  17. else {
  18. for (int binCount = 0; ; ++binCount) {
  19.                         //找到末尾位置:
  20. if ((e = p.next) == null) {
  21.                             //存放在末尾
  22. p.next = newNode(hash, key, value, null);
  23. if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
  24. treeifyBin(tab, hash); //当链表大于门槛时采用树存储。
  25. break;
  26. }
  27. if (e.hash == hash &&
  28. ((k = e.key) == key || (key != null && key.equals(k))))
  29. break;
  30. p = e;
  31. }
  32. }
  33. if (e != null) { // existing mapping for key
  34. V oldValue = e.value;
  35. if (!onlyIfAbsent || oldValue == null)
  36. e.value = value;
  37. afterNodeAccess(e);
  38. return oldValue;
  39. }
  40. }
  41. ++modCount;
  42. if (++size > threshold)
  43. resize(); //扩容链表
  44. afterNodeInsertion(evict);
  45. return null;
  46. }

4)resize,这个主要是将链表的个数增加,增加个数之后,hash规则肯定变化,所以需要重新导入一份数据到新的的table。
  1. final Node<K,V>[] resize() {
  2.         //前面一段主要是看增加到多少链表
  3. Node<K,V>[] oldTab = table;
  4. int oldCap = (oldTab == null) ? 0 : oldTab.length;
  5. int oldThr = threshold;
  6. int newCap, newThr = 0;
  7. if (oldCap > 0) {
  8. if (oldCap >= MAXIMUM_CAPACITY) {
  9. threshold = Integer.MAX_VALUE;
  10. return oldTab;
  11. }
  12. else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
  13. oldCap >= DEFAULT_INITIAL_CAPACITY)
  14. newThr = oldThr << 1; // double threshold
  15. }
  16. else if (oldThr > 0) // initial capacity was placed in threshold
  17. newCap = oldThr;
  18. else { // zero initial threshold signifies using defaults
  19. newCap = DEFAULT_INITIAL_CAPACITY;
  20. newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
  21. }
  22. if (newThr == 0) {
  23. float ft = (float)newCap * loadFactor;
  24. newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
  25. (int)ft : Integer.MAX_VALUE);
  26. }
  27. threshold = newThr;
  28. @SuppressWarnings({"rawtypes","unchecked"})
  29. Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
  30.         //移动数据开始
  31. table = newTab;
  32. if (oldTab != null) {
  33. for (int j = 0; j < oldCap; ++j) {
  34. Node<K,V> e;
  35. if ((e = oldTab[j]) != null) {
  36. oldTab[j] = null;
  37. if (e.next == null)
  38. newTab[e.hash & (newCap - 1)] = e;
  39. else if (e instanceof TreeNode)
  40. ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
  41. else { // preserve order
  42. Node<K,V> loHead = null, loTail = null;
  43. Node<K,V> hiHead = null, hiTail = null;
  44. Node<K,V> next;
  45. do {
  46. next = e.next;
  47. if ((e.hash & oldCap) == 0) {
  48. if (loTail == null)
  49. loHead = e;
  50. else
  51. loTail.next = e;
  52. loTail = e;
  53. }
  54. else {
  55. if (hiTail == null)
  56. hiHead = e;
  57. else
  58. hiTail.next = e;
  59. hiTail = e;
  60. }
  61. } while ((e = next) != null);
  62. if (loTail != null) {
  63. loTail.next = null;
  64. newTab[j] = loHead;
  65. }
  66. if (hiTail != null) {
  67. hiTail.next = null;
  68. newTab[j + oldCap] = hiHead;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return newTab;
  75. }


5)get:get是put的反过程,同样先根据hash找到分支,然后再找具体的数据。
  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. }

里面还有很多方法和内容就不全部看了,看得太多太累。懂结构就行







原创粉丝点击