这两天在复习JAVA的知识点,想更深层次的了解一下JAVA,所以就看了看JAVA的源码,把自己的分析写在这里,也当做是笔记吧,方便记忆。写的不对的地方也请大家多多指教。

  JDK1.6中HashMap采用的是位桶+链表的方式,即我们常说的散列链表的方式,而JDK1.8中采用的是位桶+链表/红黑树的方式,也是非线程安全的。当某个位桶的链表的长度达到某个阀值的时候,这个链表就将转换成红黑树。

基本的数据结构:

  1. //链表节点
  2. static class Node<K,V> implements Map.Entry<K,V> {
  3. final int hash;
  4. final K key;
  5. V value;
  6. Node<K,V> next;
  7. //省略
  8. }
  9. //红黑树节点
  10. static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
  11. TreeNode<K,V> parent; // red-black tree links
  12. TreeNode<K,V> left;
  13. TreeNode<K,V> right;
  14. TreeNode<K,V> prev; // needed to unlink next upon deletion
  15. boolean red;
  16. TreeNode(int hash, K key, V val, Node<K,V> next) {
  17. super(hash, key, val, next);
  18. }
  19. //省略
  20. }
  21. // HashMap的主要属性
  22. public class HashMap<K,V> extends AbstractMap<K,V>
  23. implements Map<K,V>, Cloneable, Serializable {
  24. // 槽数组,Node<K,V>类型,TreeNode extends LinkedHashMap.Entry<K,V>,所以可以存放TreeNode来实现Tree bins
  25. transient Node<K,V>[] table;
  26.  
  27. transient Set<Map.Entry<K,V>> entrySet;
  28.  
  29. transient int size;
  30. // 去掉了volatile的修饰符
  31. transient int modCount;
  32.  
  33. int threshold;
  34.  
  35. final float loadFactor;
  36.  
  37. ...
  38.  
  39. }
  40.   
  1. //计算key的hash
  1. static final int hash(Object key) {
  2. int h;
  3. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  4. }

get(key) 函数

  1. public V get(Object key) {
  2. Node<K,V> e;
  3. return (e = getNode(hash(key), key)) == null ? null : e.value;
  4. }
  5. final Node<K,V> getNode(int hash, Object key) {
  6. Node<K,V>[] tab;
  7. Node<K,V> first, e;
  8. int n; K k;
  9. //hash & length-1 定位数组下标
  10. if ((tab = table) != null && (n = tab.length) > 0 &&
  11. (first = tab[(n - 1) & hash]) != null)
  12. {
  13. if (first.hash == hash && // always check first node
  14. ((k = first.key) == key || (key != null && key.equals(k))))
  15. return first;
  16. if ((e = first.next) != null) {
  17. /*第一个节点是TreeNode,则采用位桶+红黑树结构,
  18. * 调用TreeNode.getTreeNode(hash,key),
  19. *遍历红黑树,得到节点的value
  20. */
  21. if (first instanceof TreeNode)
  22. return ((TreeNode<K,V>)first).getTreeNode(hash, key);
  23. do {
  24. if (e.hash == hash &&
  25. ((k = e.key) == key || (key != null && key.equals(k))))
  26. return e;
  27. } while ((e = e.next) != null);
  28. }
  29. }
  30. return null;
  31. }
  32. final TreeNode<K,V> getTreeNode(int h, Object k) {
  33. //找到红黑树的根节点并遍历红黑树
  34. return ((parent != null) ? root() : this).find(h, k, null);
  35. }
  36. /*
  37. *通过hash值的比较,递归的去遍历红黑树,这里要提的是compareableClassFor(Class k)这个函数的作用,在某些时候
  38. *如果红黑树节点的元素are of the same "class C implements Comparable<C>" type
  39. *利用他们的compareTo()方法来比较大小,这里需要通过反射机制来check他们到底是不是属于同一个类,是不是具有可比较性.
  40. */
  41. final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
  42. TreeNode<K,V> p = this;
  43. do {
  44. int ph, dir; K pk;
  45. TreeNode<K,V> pl = p.left, pr = p.right, q;
  46. if ((ph = p.hash) > h)
  47. p = pl;
  48. else if (ph < h)
  49. p = pr;
  50. else if ((pk = p.key) == k || (k != null && k.equals(pk)))
  51. return p;
  52. else if (pl == null)
  53. p = pr;
  54. else if (pr == null)
  55. p = pl;
  56. else if ((kc != null ||
  57. (kc = comparableClassFor(k)) != null) &&
  58. (dir = compareComparables(kc, k, pk)) != 0)
  59. p = (dir < 0) ? pl : pr;
  60. else if ((q = pr.find(h, k, kc)) != null)
  61. return q;
  62. else
  63. p = pl;
  64. } while (p != null);
  65. return null;
  66. }

put(K key,V value)函数

  1. //put(K key,V value)函数
  2. public V put(K key, V value) {
  3. return putVal(hash(key), key, value, false, true);
  4. }
  5.  
  6. final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
  7. boolean evict) {
  8. Node<K,V>[] tab;
  9. Node<K,V> p;
  10. int n, i;
  11. //如果table为空或者长度为0,则resize()
  12. if ((tab = table) == null || (n = tab.length) == 0)
  13. n = (tab = resize()).length;
  14. //找到key值对应的槽并且是第一个,直接加入
  15. if ((p = tab[i = (n - 1) & hash]) == null)
  16. tab[i] = newNode(hash, key, value, null);
  17. else {
  18. Node<K,V> e;
  19. K k;
  20. //第一个node的hash值即为要加入元素的hash
  21. if (p.hash == hash &&
  22. ((k = p.key) == key || (key != null && key.equals(k)))){
  23. e = p;
  24. }else if (p instanceof TreeNode)//第一个节点是TreeNode,即tree-bin
  25. /*Tree version of putVal.
  26. *final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,int h, K k, V v)
  27. */
  28. e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
  29. else {
  30. //不是TreeNode,即为链表,遍历链表
  31. for (int binCount = 0; ; ++binCount) {
  32. /*到达链表的尾端也没有找到key值相同的节点,
  33. *则生成一个新的Node,并且判断链表的节点个数是不是到达转换成红黑树的上界
  34. *达到,则转换成红黑树
  35. */
  36. if ((e = p.next) == null) {
  37. p.next = newNode(hash, key, value, null);
  38. if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
  39. treeifyBin(tab, hash);
  40. break;
  41. }
  42. if (e.hash == hash &&
  43. ((k = e.key) == key || (key != null && key.equals(k))))
  44. break;
  45. p = e;
  46. }
  47. }
  48. if (e != null) { // existing mapping for key
  49. V oldValue = e.value;
  50. if (!onlyIfAbsent || oldValue == null)
  51. e.value = value;
  52. afterNodeAccess(e);
  53. //返回旧的value值
  54. return oldValue;
  55. }
  56. }
  57. ++modCount;
  58. if (++size > threshold)
  59. resize();
  60. afterNodeInsertion(evict);
  61. return null;
  62. }

java HashMap源码分析(JDK8)的更多相关文章

  1. Java HashMap源码分析(含散列表、红黑树、扰动函数等重点问题分析)

    写在最前面 这个项目是从20年末就立好的 flag,经过几年的学习,回过头再去看很多知识点又有新的理解.所以趁着找实习的准备,结合以前的学习储备,创建一个主要针对应届生和初学者的 Java 开源知识项 ...

  2. Java HashMap源码分析

    貌似HashMap跟ConcurrentHashMap是面试经常考的东西,抽空来简单分析下它的源码 构造函数 /** * Constructs an empty <tt>HashMap&l ...

  3. 源码分析(一) HashMap 源码分析|JDK8

    HashMap是一个普遍应用于各大JAVA平台的最最最常用的数据结构.<K,V>的存储形式使HashMap备受广大java程序员的喜欢.JDK8中HashMap发生了很大的变化,例如:之前 ...

  4. 【JAVA集合】HashMap源码分析(转载)

    原文出处:http://www.cnblogs.com/chenpi/p/5280304.html 以下内容基于jdk1.7.0_79源码: 什么是HashMap 基于哈希表的一个Map接口实现,存储 ...

  5. Java源码解析——集合框架(五)——HashMap源码分析

    HashMap源码分析 HashMap的底层实现是面试中问到最多的,其原理也更加复杂,涉及的知识也越多,在项目中的使用也最多.因此清晰分析出其底层源码对于深刻理解其实现有重要的意义,jdk1.8之后其 ...

  6. java集合源码分析(六):HashMap

    概述 HashMap 是 Map 接口下一个线程不安全的,基于哈希表的实现类.由于他解决哈希冲突的方式是分离链表法,也就是拉链法,因此他的数据结构是数组+链表,在 JDK8 以后,当哈希冲突严重时,H ...

  7. Java集合源码分析(四)HashMap

    一.HashMap简介 1.1.HashMap概述 HashMap是基于哈希表的Map接口实现的,它存储的是内容是键值对<key,value>映射.此类不保证映射的顺序,假定哈希函数将元素 ...

  8. 【Java】HashMap源码分析——常用方法详解

    上一篇介绍了HashMap的基本概念,这一篇着重介绍HasHMap中的一些常用方法:put()get()**resize()** 首先介绍resize()这个方法,在我看来这是HashMap中一个非常 ...

  9. 【Java】HashMap源码分析——基本概念

    在JDK1.8后,对HashMap源码进行了更改,引入了红黑树.在这之前,HashMap实际上就是就是数组+链表的结构,由于HashMap是一张哈希表,其会产生哈希冲突,为了解决哈希冲突,HashMa ...

随机推荐

  1. (并查集 添加关系)How Many Answers Are Wrong --Hdu --3038

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. Robotframework-Appium 之常用API(二)

    续接上一文,更多API详细如下: 注:更多官方详情信息见 http://robotframework.org/robotframework/ 28. Name: Install App Source: ...

  3. 20155209 2016-2017-2 《Java程序设计》第八周学习总结

    20155209 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从J ...

  4. android免root hook框架legend

    一.前言 Android中hook框架已经非常多了,最优秀的当属Xposed和Substrate了,这两个框架我在之前的文章都详细介绍过了,不了解的同学,可以转战这里:http://www.wjdia ...

  5. Gridview的RowDataBound事件可以做很多事情

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)        {            //如果是绑 ...

  6. Amazon成本和产出的衡量方式

    Amazon用一种T-Shirt Size 估计的方式来做项目. 产品经理会对每一条需求评估上业务影响力的尺寸,如:XXXL 影响一千万人以上或是可以占到上亿美金的市场,XXL,影响百万用户或是占了千 ...

  7. 查看Linux服务器被映射的公网ip

    查看Linux服务器被映射的公网ip   现在云服务器非常流行,不仅企业甚至是个人都可能拥有自己的云服务器,但是目前的云服务器厂商提供的公网IP大都是映射而来,所以在Linux服务器上执行ifconf ...

  8. [ACM_模拟] HDU 1006 Tick and Tick [时钟间隔角度问题]

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  9. LeetCode145:Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  10. javascript快速排序的思考

    还记得三个月前,学习过快速排序,示例所讲的python快速排序十分易于理解,然而网上学习的c#的快速排序当时就懵逼的,现在已经全忘了,大概记得个思路 在学习完了一些高级的js方法后,今天用js模拟了p ...