我是靠谱客的博主 曾经雪碧,这篇文章主要介绍Java遍历时删除List、Set、Map中的元素(源码分析)ArrayList源码中说明的报异常原因:代码示例:,现在分享给大家,希望可以做个参考。

在对List、Set、Map执行遍历删除或添加等改变集合个数的操作时,不能使用普通的while、for循环或增强for。会抛出ConcurrentModificationException异常或者没有达到删除的需求。在遍历时删除元素,需要使用迭代器的方式。

ArrayList源码中说明的报异常原因:

 * <p>The iterators returned by this class's <tt>iterator</tt> and
 * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is
 * structurally modified at any time after the iterator is created, in any way
 * except through the iterator's own <tt>remove</tt> or <tt>add</tt> methods,
 * the iterator will throw a {@link 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.<p>

(翻译:通过类的iterator和listiterator方法获取到的迭代器是快速失败迭代器:如果list在迭代器生成之后发生了结构性的改变,迭代器将抛出ConcurrentModificationException,但是当使用迭代器自己的remove或add方法时,不会抛出此异常。也就是说,当面对并发修改时,迭代器快速失败,而不是冒在未来不确定的时间发生不确定的行为的危险。)
 *
 * Note that the fail-fast behavior of an iterator cannot be guaranteed
 * as it is, generally speaking, impossible to make any hard guarantees in the
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
 * Therefore, it would be wrong to write a program that depended on this
 * exception for its correctness: <i>the fail-fast behavior of iterators
 * should be used only to detect bugs.</i><p>

(翻译:需要注意的是迭代器不保证快速失败行为一定发生,因为一般来说不可能对是否发生了不同步并发修改做任何硬性的保证。快速失败迭代器会尽最大努力抛出ConcurrentModificationException异常。因此,写一个通过是否出现这种异常来判断是否正确的程序是错误的。快速失败行为的正确用法是仅用于检测异常。)


代码示例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class CollectionRemoveDemo { public static void main(String[] args) { ListRemove(); System.out.println("-----------------------------------------------------------------------------------------------"); SetRemove(); System.out.println("-----------------------------------------------------------------------------------------------"); MapRemove(); } public static void ListRemove(){ List<String> strList = new ArrayList<String>(); strList.add("aaaa"); strList.add("bbbb"); strList.add("cccc"); strList.add("cccc"); strList.add("dddd"); for(String str : strList){ System.out.println(str); } System.out.println("init List size:" + strList.size()); Iterator<String> it = strList.iterator(); while(it.hasNext()){ String str = it.next(); if(str.equals("cccc")){ it.remove(); } } for(String str : strList){ System.out.println(str); } System.out.println("removed List size:" + strList.size()); } public static void SetRemove(){ Set<String> strSet = new TreeSet<String>(); strSet.add("aaaa"); strSet.add("bbbb"); strSet.add("cccc"); strSet.add("cccc");//重复的数据将不会再次插入 strSet.add("dddd"); for(String str : strSet){ System.out.println(str); } System.out.println("Init Set size:" + strSet.size()); Iterator<String> it = strSet.iterator(); while(it.hasNext()){ String str = it.next(); if(str.equals("cccc")){ it.remove(); } } for(String str : strSet){ System.out.println(str); } System.out.println("removed Set size:" + strSet.size()); } public static void MapRemove(){ Map<String, String> strMap = new TreeMap<String, String>(); strMap.put("a", "aaaa"); strMap.put("b", "bbbb"); strMap.put("c", "cccc"); strMap.put("d", "dddd"); for(String key : strMap.keySet()){ System.out.println(key + " : " + strMap.get(key)); } System.out.println("Init Map size:" + strMap.size()); Iterator<Entry<String,String>> it = strMap.entrySet().iterator(); while(it.hasNext()){ Entry<String,String> strEntry = it.next(); if(strEntry.getKey().equals("c")){ it.remove(); } } for(String key : strMap.keySet()){ System.out.println(key + " : " + strMap.get(key)); } System.out.println("removed Map size:" + strMap.size()); } }


最后

以上就是曾经雪碧最近收集整理的关于Java遍历时删除List、Set、Map中的元素(源码分析)ArrayList源码中说明的报异常原因:代码示例:的全部内容,更多相关Java遍历时删除List、Set、Map中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部