本文源码基于JDK1.8.0_45。

 final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
//如果HashMap未初始化或哈希值对应下标无节点,不需要执行移除操作
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
//头节点等于目标元素
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
//如果是树结构,调用红黑树的接口获取目标节点
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
//链表结构,遍历整个链表
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
//如果移除前需要判断节点的值,则目标值与原值相等才能移除
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
//调用红黑树的接口移除树节点
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
//如果目标元素等于头节点的值,移除对头节点的引用,指向下一个节点
else if (node == p)
tab[index] = node.next;
//如果是链表结构,则前序节点的next指向要移除节点的下一个节点
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}

最新文章

  1. 总结项目开发中用到的一些css\html技巧
  2. HttpWebRequest调用WebAPI
  3. CentOS7服务器相关配置
  4. HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示
  5. 西秦的ACE-Python教程 一、Python本地开发环境部署
  6. 关于 C/C++ 的文章
  7. hbase使用-java操作
  8. oc-05-对象的创建
  9. adobe 蛋疼的套装, 想安装一个Flash Professional CS6,标准版还没有...
  10. git configuration
  11. Mysql数据库中的EXISTS和NOT EXISTS
  12. XML文件编码问题
  13. FineReport单元格扩展与父子格设置
  14. [python学习笔记] py2exe 打包
  15. PHP读取数据库表显示到前台
  16. BZOJ4711 小奇挖矿
  17. Scala中foldLeft的总结
  18. 引入CSS的三种方式
  19. vi/vim键盘对应图
  20. VS2010发布、打包安装程序(超全超详细)

热门文章

  1. C基础-对malloc的使用与理解
  2. FJOI2019退役记
  3. ACM_括号匹配
  4. ACM_错排(递推dp)
  5. [转]ASP .NET MVC 之Entity Framework- code first
  6. IE 浏览器在地址栏输入中文字符,发送get请求报400错误的问题
  7. Linux 学习(三)
  8. CNN结构:可用于时序预测复合的DNN结构-AcGANs、误差编码网络 ENN
  9. c++中的类型转换--reinterpret_cast
  10. 在eclipse里如何快速定位到某一行?