在以下四种遍历过程中,前两种会抛出ConcurrentModificationException,而后两种方法是正确的.
Department类:
package com.sitinspring;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Department {
private String name;
private List<Member> memberSheet;
public Department(String name) {
this.name = name;
}
public void addMemer(Member member) {
if (memberSheet == null) {
memberSheet = new ArrayList<Member>();
}
memberSheet.add(member);
}
public void printMemberSheet() {
System.out.println("----部门" + name + "人员名单---");
for (Member member : memberSheet) {
System.out.println(member);
}
}
/**
* 里面的四个清除过程请分别独立执行
*
*/
public void removeYoungerFromMemberSheet() {
// 遍历一:这个处理会抛出java.util.ConcurrentModificationException
for (Member member : memberSheet) {
if (member.getAge() < 30) {
memberSheet.remove(member);
}
}
// 遍历二:这个处理也会抛出java.util.ConcurrentModificationException
for (Iterator it = memberSheet.iterator(); it.hasNext();) {
Member member = (Member) it.next();
if (member.getAge() < 30) {
memberSheet.remove(member);
}
}
// 遍历三:这个处理调用Iterator 本身的方法 remove(),会正常执行
for (Iterator it = memberSheet.iterator(); it.hasNext();) {
Member member = (Member) it.next();
if (member.getAge() < 30) {
it.remove();
}
}
// 遍历四:这个处理不依赖Iterator,也会正常执行
for (int i=0;i<memberSheet.size();i++) {
Member member = memberSheet.get(i);
if (member.getAge() < 30) {
memberSheet.remove(member);
}
}
}
public String toString() {
return name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Department resarchDept = new Department("研发部门");
resarchDept.addMemer(new Member("张三", 38));
resarchDept.addMemer(new Member("李四", 24));
resarchDept.addMemer(new Member("王五", 30));
resarchDept.addMemer(new Member("钱七", 22));
resarchDept.addMemer(new Member("孙八", 39));
resarchDept.addMemer(new Member("周九", 30));
resarchDept.removeYoungerFromMemberSheet();
resarchDept.printMemberSheet();
}
}
Member类:
package com.sitinspring;
public class Member{
private String name;
private int age;
private Department department;
public Member(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
StringBuffer sb=new StringBuffer();
sb.append("员工名="+name);
sb.append(" 年龄="+age);
if(department!=null){
sb.append(" 所属部门="+department);
}
return sb.toString();
}
public void changeNewDepartment(Department newDepartment) {
department.removeMemer(name);
newDepartment.addMemer(this);
}
public String getName() {
return name;
}
public void setDepartment(Department department) {
this.department = department;
}
}
为什么会发生这样的结果呢?这是因为
"
当使用 fail-fast iterator 对 Collection 或 Map 进行迭代操作过程中尝试直接修改 Collection / Map 的内容时,即使是在单线程下运行, java.util.ConcurrentModificationException 异常也将被抛出。
Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。
所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。"
上述这段资料来自http://hi.baidu.com/xjenator/blog/item/23b235a89041d4b0ca130c16.html.
java.util包中很多迭代器都是所谓的fail-fast迭代器.这些迭代器如果发现集合被修改,而且不是通过迭代器本身,那么抛出一个异常进行清除-
ConcurrentModificationException-从而避免不安全行为的发生.
因此,第三种采用it.remove();不会出现任何异常,而第四不依赖于Iterator而依赖于索引当然更不会出现异常.
代码下载:
http://www.blogjava.net/Files/sitinspring/ConcurrentModificationTest20071203210937.rar