参见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.ui
针对其余list等集合,遍历过程当中的删除操做,也须要使用迭代删除this
private static Map<Integer, String> map=new HashMap<Integer,String>(); 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<Map.Entry<Integer, String>> iter = map.entrySet().iterator(); while(iter.hasNext()){ Map.Entry<Integer, String> 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<Integer, String> entry:map.entrySet()){ int k=entry.getKey(); String v=entry.getValue(); System.out.println(k+" = "+v); } }
在main方法中运行 iterTest()
,输出结果为:code
------- 最终的map的元素遍历: 2 = two 4 = four 6 = six 8 = eight 10 = ten
若将three
iter.remove();
替换成rem
map.put(key, "奇数"); 或者 map.remove(key);
则会报出 ConcurrentModificationException
异常文档