map遍历删除异常:ConcurrentModificationException
来源:互联网 发布:淘宝网页版登录首页 编辑:程序博客网 时间:2024/05/18 01:11
1. map集合单个删除
此时,一般都不会出问题,直接 remove(key)
2. map集合遍历删除多个
此时,若不注意往往容易出现问题,出现 ConcurrentModificationException
为什么呢?
使用iterator迭代删除时没有问题的,在每一次迭代时都会调用hasNext()方法判断是否有下一个,是允许集合中数据增加和减少的。
使用forEach删除时,会报错ConcurrentModificationException,因为在forEach遍历时,是不允许map元素进行删除和增加。
所以,遍历删除map集合中元素时,必须使用迭代iterator
3. 案例
有如下集合:
Map<String, Integer> map=new HashMap<>(); map.put("张三",22); map.put("李四",25); map.put("王五",33); map.put("赵六",28); map.put("田七",25); map.put("李思",25); map.put("李嘉欣",25);
需求:删除名字(即key)中包含“李”的元素
使用forEach时:
Set<Entry<String, Integer>> set=map.entrySet();for (Entry<String, Integer> entry : set) { String name=entry.getKey(); System.out.println(name); System.out.println(name.contains("李")); if(name.contains("李")){ map.remove(name); }}
会报如下异常:
使用iterator时,一切正常
特别注意:删除时不能使用map.remove(key),否则也报ConcurrentModificationException
只能使用iterator.remove
Set<Entry<String, Integer>> set=map.entrySet();Iterator<Entry<String, Integer>> iterator=set.iterator();while(iterator.hasNext()){ Entry<String, Integer> entry=iterator.next(); String name=entry.getKey(); if(name.contains("李")){ //特别注意:不能使用map.remove(name) 否则会报同样的错误 iterator.remove(); }}
解释:Iterator.remove()是删除最近(最后)使用next()方法的元素。
从迭代器指向的集合中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。
完成代码:
package map遍历和删除;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;/*map集合遍历删除*/public class Test2 { public static void main(String[] args) { //key=name value=age Map<String, Integer> map=new HashMap<>(); map.put("张三",22); map.put("李四",25); map.put("王五",33); map.put("赵六",28); map.put("田七",25); map.put("李思",25); map.put("李嘉欣",25); //单个删除删--remove map.remove("张三"); for(Map.Entry<String, Integer> entry:map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } //需求:删除名字(即key)中包含“李”的元素 /*分析: * 1.此时直接map.remove(key)就不符合要求了,必须进行遍历删除 * * 2.通常map集合遍历就两种方式,一个foreach和iterator * */ Set<Entry<String, Integer>> set=map.entrySet(); /*for (Entry<String, Integer> entry : set) { String name=entry.getKey(); System.out.println(name); System.out.println(name.contains("李")); if(name.contains("李")){ map.remove(name); } }*/ Iterator<Entry<String, Integer>> iterator=set.iterator(); while(iterator.hasNext()){ Entry<String, Integer> entry=iterator.next();// String name=entry.getKey(); String name=entry.getKey(); int value=entry.getValue(); if(name.contains("李")){ //特别注意:不能使用map.remove(name) 否则会报同样的错误 iterator.remove(); } } System.out.println(); for (Entry<String, Integer> entry : set) { System.out.println("姓名:"+entry.getKey()+",年龄:"+entry.getValue()); } }}
阅读全文
0 0
- map遍历删除异常:ConcurrentModificationException
- 遍历Map时抛出java.util.ConcurrentModificationException异常的解决办法
- 遍历Map时抛出java.util.ConcurrentModificationException异常的解决办法
- Map中删除数据报:java.util.ConcurrentModificationException异常
- 迭代器遍历:ConcurrentModificationException异常
- 遍历集合ConcurrentModificationException异常
- Map遍历时报ConcurrentModificationException解决方法
- java.util.ConcurrentModificationException---用迭代器遍历Map时删除其中元素出现异
- 关于Map表循环过程删除操作出现的java.util.ConcurrentModificationException 异常
- 关于Map表循环过程删除操作出现的java.util.ConcurrentModificationException 异常
- 关于map中删除value为空,出现异常java.util.ConcurrentModificationException
- JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常
- java遍历集合删除特定元素的 java.util.ConcurrentModificationException异常
- ArrayList循环遍历并删除元素时报java.util.ConcurrentModificationException异常
- Java 遍历List ConcurrentModificationException 异常解析
- ArrayList删除元素并发异常ConcurrentModificationException解决方案
- map循环遍历删除
- ConcurrentModificationException 异常
- 《使用Python进行自然语言处理》学习笔记四
- Bash scripts-II
- 符号表
- SpringBoot系列之三全局异常的捕获处理
- 二级列表实现CheckBox单选及全选
- map遍历删除异常:ConcurrentModificationException
- java值传递和引用传递
- 萌新程序媛笔记-第1章-程序设计入门
- 计算几何练习1
- 火车进站 一个入栈序列的所有出栈顺序
- 《程序员面试宝典第四版》笔记3
- 今天来聊聊序列化与烦序列化
- windows-caffe入门之用CPU训练自己的数据
- Java基础学习笔记 第二部分 part 2