Java Map在遍历过程中删除元素
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在遍历过程中删除元素的更多相关文章
- java list集合遍历时删除元素
转: java list集合遍历时删除元素 大家可能都遇到过,在vector或arraylist的迭代遍历过程中同时进行修改,会抛出异常java.util.ConcurrentModification ...
- JAVA List 一边遍历一边删除元素
JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常 2015年02月10日 14:42:49 zhanzkw 阅读数:3 ...
- 遍历List过程中删除元素的正确做法(转)
遍历List过程中删除元素的正确做法 public class ListRemoveTest { 3 public static void main(String[] args) { 4 ...
- Jquery中删除元素方法
empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除 语法: empty() remove(expr); empty用来删除指定元素的子元素,remove用来删除元素 ...
- /编写一个函数,要求从给定的向量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:$ */ /* 编写一个 ...
- 如何从List中删除元素
从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo { public static void main(Str ...
- PHP从数组中删除元素的方法
PHP从数组中删除元素的方法 本篇文章主要介绍了PHP从数组中删除元素的四种方法实例 删除一个元素,且保持原有索引不变 使用 unset 函数,示例如下: 1 2 3 4 5 <?php $ ...
- PHP从数组中删除元素的四种方法实例
PHP从数组中删除元素的四种方法实例 一.总结 一句话总结:unset(),array_splice(),array_diff(),array_diff_key() 二.PHP从数组中删除元素的四种方 ...
- MongoDB 学习笔记之 从数组中删除元素和指定数组位置
从数组中删除元素: 从数组中删除单个元素: db.ArrayTest.updateOne({ "name" : "Bill"},{$pop: {"ad ...
随机推荐
- 【译】8. Java反射——注解
原文地址:http://tutorials.jenkov.com/java-reflection/annotations.html ================================== ...
- jquery 获取$("#id").text()里面的值 需要进行去空格去换行符操作
Jquery:$("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));//去 ...
- sqlserver2008查看表记录或者修改存储过程出现目录名无效错误解决方法
登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,现在把解决方法分享给大家 1.新建查询 2.点工具栏中[显示估计的查询计划],结果提示Documents and Settings ...
- (二叉树 BFS) leetcode513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- php的扩展配置
第一步:在php的安装目录下找到php.ini-development这个文件,并复制一个副本,修改这个副本的文件名为:php.ini 如下图我的目录为: 第二步:打开这个php.ini,修改 将这里 ...
- python自动化开发-[第二天]-基础数据类型与编码(续)
今日简介: - 编码 - 进制转换 - 初识对象 - 基本的数据类型 - 整数 - 布尔值 - 字符串 - 列表 - 元祖 - 字典 - 集合 - range/enumcate 一.编码 encode ...
- How-to: Do Statistical Analysis with Impala and R
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- python (大文件下载及进度条展示) 验证客户端链接的合法性,socketserver
##########总结########### 文件校验加进度条显示 ####server import os import json import socket import struct impo ...
- js实现table用鼠标改变td的宽度,固定table宽度和高度超过显示点
<!DOCTYPE HTML> <html> <head> <meta charset="gbk"> <title>ta ...
- Golang入门教程(四)变量声明
Go 语言变量名由字母.数字.下划线组成,其中首个字母不能为数字. 一.Go的语言结构 1.Go的语言基础部分由这几个部分组成 包声明 引入包 函数 变量 语句&表达式 注释 比如下面这个简单 ...