java map遍历删除_Java Map在遍历过程中删除元素
map遍历判断筛选删除时
如果对map使用put、remove或clear方法(例如map.remove直接删除),那么迭代器就不再合法(并且在其后使用该迭代器将会有ConcurrentModificationException异常被抛出).
当Iterator.remove方法导致map发生变化时,他会更新cursor来同步这一变化。
参见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.
结论: 应该使用迭代删除
推广
针对其他list等集合,遍历过程中的删除操作,也需要使用迭代删除
测试demo
private static Map map = new HashMap();
public static void iterTest() {
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
map.put(5, "five");
map.put(9, "nine");
map.put(10, "ten");
Iterator> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = iter.next();
int key = entry.getKey();
if (key % 2 == 1) {
System.out.println("delete this: " + key + " = " + key);
//map.put(key, "奇数"); //ConcurrentModificationException
//map.remove(key); //ConcurrentModificationException
iter.remove(); //OK
}
}
//遍历当前的map;这种新的for循环无法修改map内容,因为不通过迭代器。
System.out.println("-------\n\t最终的map的元素遍历:");
for (Map.Entry entry : map.entrySet()) {
int k = entry.getKey();
String v = entry.getValue();
System.out.println(k + " = " + v);
}
}
在main方法中运行 iterTest() ,输出结果为:
-------
最终的map的元素遍历:
2 = two
4 = four
6 = six
8 = eight
10 = ten
若将
iter.remove();
替换成
map.put(key, "奇数");
或者 map.remove(key);
则会报出 ConcurrentModificationException 异常
java map遍历删除_Java Map在遍历过程中删除元素相关推荐
- mysql for 循环删除_Java增强for循环中删除元素抛异常问题
前言 最近突然想起刚毕业那会找工作时面试被问了个这样的问题.就是"使用增强for循环遍历ArrayList(List集合)时删除其中的元素是否会出现异常?".说实话当时真把我愣住了 ...
- List与Map的遍历过程中删除元素
在日常的开发过程中,经常需要对List或Map里面的符合某种业务的数据进行删除,但是如果不了解里面的机制就容易掉入"陷阱"导致遗漏或者程序异常. List遍历过程中删除元素 使用索 ...
- cte公用表表达式_CTE SQL删除; 在SQL Server中删除具有公用表表达式的数据时的注意事项
cte公用表表达式 In this article, the latest in our series on Common table expressions, we'll review CTE SQ ...
- java如何遍历字典_Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- java map 自动排序_java Map排序问题
java 中,Map常见的有HashMap ,TreeMap等等,Map是一个接口,我们不能直接声明一个Map类型的对象,在实际开发 中,比较常用的Map性数据结构是HashMap和TreeMap,它 ...
- java从map取值_java map中怎么通过键取出值?
展开全部 在java map中取出键值有以32313133353236313431303231363533e59b9ee7ad9431333363396464下两种方法: 1.第一种方法根据键值的名字 ...
- java map null吗_Java: Map里面的键和值可以为空吗?
在Java中,Map里面的键和值可以为空吗?我们先来看一个例子: private static void TestHashMap() { // TODO Auto-generated method s ...
- java list遍历添加元素_java遍历List过程中添加和删除元素的问题
遍历元素最常见的三种方法: //第三种遍历[利用迭代器] private static void loopList3(List strList) { Iterator itr = strList.it ...
- java map key 升序_Java Map 按 key 升序排序
最近开发微信和支付宝的服务端支付,涉及到字典的排序和 url 参数转换成字典的操作,整理了一个工具类: import java.util.ArrayList; import java.util.Col ...
最新文章
- python第一次使用教程-Python考试_第一次
- RDLC 2010设计器的数据源无法找到静态类作为数据源
- config中自定义配置
- 01、python数据分析与机器学习实战——Python科学计算库-Numpy
- Python Pytest前置setup和后置teardown详解
- Nginx进程以及事件处理模型
- node.js学习笔记之写文件
- 记录——《C Primer Plus (第五版)》第八章编程练习第八题
- 2022-03-13 转载 Dockerfile 高阶使用指南及镜像优化
- 模块化方案esl以及amd的依赖方式
- 这些JAVA毕业设计拿走不谢
- 使用 NVIDIA Kaolin Wisp 重建3D场景
- GEE-Python遥感大数据分析、管理与可视化实践技术
- 删除磁盘管理中的OEM分区
- 【BFS】大胖子走迷宫
- [CSP冲刺班]CSP-J2021模拟赛#9
- iota 的 优点与吐槽
- 武汉大学IT职业培训
- Camera 冷启动阶段分解
- 超级全的PCB LAYOUT高速信号走线指南