HashMap

前置条件

  1. 了解数组
  2. 了解链表
  3. jdk version: 1.8

个人分3步来了解HashMap

  1. 通过数据结构图
  2. 通过为了完成这样的数据结构我们该怎么做
  3. HashMap 实际put方法源码追踪

先通过一张数据结构图对HashMap的数据结构进行一个简单了解

HashMap的put方法-UML图

这只是uml图算是对HashMap的put操作应该做的事情的一个基本推导图

HashMap put方法代码

public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
} 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)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
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);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
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;
}

最新文章

  1. HDU1039-Easier Done Than Said?(字符串处理)
  2. 浅析敏感词过滤算法(C++)
  3. rpc框架之 thrift 学习 2 - 基本概念
  4. jQuery中$.ajax()和$.getJson()同步处理详解
  5. shell date
  6. Xamarin 安装体验
  7. Android Mediaplayer各种属性和方法简单介绍
  8. 左右v$datafile和v$tempfile中间file#
  9. 201521123114 《Java程序设计》第7周学习总结
  10. 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解
  11. 利用mysqldump命令导出为csv格式文件
  12. idea生成类注释和方法注释的正确方法
  13. Windows server 2016 安装 TFS
  14. 用actor model实现intel tbb这样的用法
  15. STM32F103 TIM3定时器初始化程序
  16. 协程(Coroutine)并不是真正的多线程
  17. Java之创建对象&gt;7.Avoid finalizers
  18. centos7配置vsftpd服务器
  19. gflags摘记
  20. SQL语言基本操作(聚合函数)

热门文章

  1. Counting blessings can actually increase happiness and health by reminding us of the good things in life.
  2. JAVA方法定义和调用
  3. ubuntu双屏调整分辨率
  4. 在vue-cli中引入图片不能正常显示
  5. linux命令 ——目录
  6. Android中的Matrix(矩阵)
  7. android RadioGroup设置某一个被选中
  8. 【HHHOJ】NOIP模拟赛 玖 解题报告
  9. 文件系统 - Linux 支持的文件系统类型
  10. angular路由学习笔记