Java中的Map如果在遍历过程中要删除元素,除非通过迭代器自己的remove()方法,否则就会导致抛出ConcurrentModificationException异常。JDK文档中是这么描述的:

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
译文:这个类的“集合视图方法”返回的迭代器是快速失败的:如果在创建迭代器之后的任何时候结构上都被修改了,那么除了通过迭代器自己的删除方法外,迭代器将抛出ConcurrentModificationException。因此,在面对并发修改时,迭代器会快速而干净地失败,而不会在将来的某个不确定的时间冒险使用任意的、不确定的行为。

这么做的原因是为了保证迭代器能够尽快感知到Map的“结构性修改“,从而避免不同视图下不一致现象

public class HashTest {
public static void main(String[] args) {
HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
count.put(1, 11);
count.put(2, 22);
count.put(3, 33);
//错误的 会抛出异常 ----ConcurrentModificationException
for (Integer i : count.keySet()) {
if(i == 2){
count.remove(i);
}
System.out.println(i);
}
//正确的
Iterator<Integer> it = count.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
if(key == 3){
count.put(key, 44);
}
} for (Integer value : count.values()) {
System.out.println(value);
}
}
}

Java Map在遍历过程中删除元素的更多相关文章

  1. java list集合遍历时删除元素

    转: java list集合遍历时删除元素 大家可能都遇到过,在vector或arraylist的迭代遍历过程中同时进行修改,会抛出异常java.util.ConcurrentModification ...

  2. JAVA List 一边遍历一边删除元素

    JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常 2015年02月10日 14:42:49 zhanzkw 阅读数:3 ...

  3. 遍历List过程中删除元素的正确做法(转)

    遍历List过程中删除元素的正确做法   public class ListRemoveTest {     3 public static void main(String[] args) { 4 ...

  4. Jquery中删除元素方法

    empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除 语法: empty() remove(expr); empty用来删除指定元素的子元素,remove用来删除元素 ...

  5. /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数

    /** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...

  6. 如何从List中删除元素

    从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo {     public static void main(Str ...

  7. PHP从数组中删除元素的方法

    PHP从数组中删除元素的方法 本篇文章主要介绍了PHP从数组中删除元素的四种方法实例 删除一个元素,且保持原有索引不变 使用 unset 函数,示例如下: 1 2 3 4 5 <?php   $ ...

  8. PHP从数组中删除元素的四种方法实例

    PHP从数组中删除元素的四种方法实例 一.总结 一句话总结:unset(),array_splice(),array_diff(),array_diff_key() 二.PHP从数组中删除元素的四种方 ...

  9. MongoDB 学习笔记之 从数组中删除元素和指定数组位置

    从数组中删除元素: 从数组中删除单个元素: db.ArrayTest.updateOne({ "name" : "Bill"},{$pop: {"ad ...

随机推荐

  1. react-native中的触摸事件

    移动应用上的用户交互基本靠"摸".当然,"摸"也是有各种姿势的:在一个按钮上点击,在一个列表上滑动, 或是在一个地图上缩放.React Native 提供了可以 ...

  2. Python之迭代器,生成器

    迭代器 1.什么是可迭代对象 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. from collections import Iterable l = [1,2,3,4] t ...

  3. tensor flow中summary用法总结

    对于用法的总结详细的参见博文https://www.cnblogs.com/lyc-seu/p/8647792.html

  4. 查询redis数据

    1.连接跳板机 2.跳板机连接服务器 3.服务器打开redis 4.查询redis数据

  5. Cookie隐藏小广告

    方式一:在close.php页面上设置COOKIE, colse.php页面 <?php setcookie('hide','1'); header('Location: indes.php') ...

  6. (转载) python3: beautifulsoup的使用

    转载: https://www.cnblogs.com/chimeiwangliang/p/8649003.htmlfrom bs4 import BeautifulSoup import reque ...

  7. 苹果笔记本适合什么人 中国Mac电脑用户的8个事实

    报告由腾讯 ISUX 研究中心收集了全国 7946 名 Mac 电脑用户的问卷整理而成.并且,参考了苹果公司的历年财报,以及百度.StatCounter 等第三方市场统计数据. 你是 iPhone 用 ...

  8. bzoj2938 AC自动机 + 拓扑排序找环

    https://www.lydsy.com/JudgeOnline/problem.php?id=2938 题意:给出N个01病毒序列,询问是否存在一个无限长的串不存在病毒序列 正常来说,想要寻找一个 ...

  9. Go多组Raft库

    Go多组Raft库 https://github.com/lni/dragonboat/blob/master/README.CHS.md 使用用例 https://github.com/lni/dr ...

  10. 2017-12-15python全栈9期第二天第五节之while else的用法一当被break打断时else内容的结果不会被打印

    #!/user/bin/python# -*- coding:utf-8 -*-count = 0while count <5 : count += 1 if count == 3 : brea ...