map遍历删除异常:ConcurrentModificationException:null

遍历循环 map的时候,把不符合条件的值删除,会报异常

java.util.ConcurrentModificationException: null

原因是 导致modCount和expectedModCount的值不一致
起初 是这样写的:

for(Map.Entry entry:EmployeeAppAct.keySet.entrySet()){
	Date time = (Date) entry.getValue();
	int minuteBetWeen = DateCommon.daysBetween(time, new Date());
	if(minuteBetWeen>=CommonConstans.QRCODE_TIME_OUT){
		EmployeeAppAct.keySet.remove(entry.getKey());
		System.out.println(EmployeeAppAct.keySet);
	}

结果抛了异常
借鉴网上的做法之后,我改成了:

while(it.hasNext()){
	Entry entry = it.next();
	Date time = (Date) entry.getValue();
	int minuteBetWeen = DateCommon.daysBetween(time, new Date());
	if(minuteBetWeen>=CommonConstans.QRCODE_TIME_OUT){
		it.remove();
	}
}

就可以了     笔芯

你可能感兴趣的:(异常及错误)