今天在遍历list集合时同时对list集合进行增加和删除操作,导致了以下错误:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at com.cdsoft.epcp.android.InstanceManagerAndroidController$1.run(InstanceManagerAndroidController.java:546)
代码如下:
List<String> endTimeList = new ArrayList<>();
List<OperateCameraInfo> withSameIpCameraInfos = new ArrayList<>();
for (int i = 0; i < operateCameraInfoList.size(); i++) {
if (i == 0) {
withSameIpCameraInfos.add(operateCameraInfoList.get(i));
}
if (i != 0 && i == (operateCameraInfoList.size() - 1)) {
withSameIpCameraInfos.add(operateCameraInfoList.get(i));
endTimeList.add(audioCameraInfos.get(audioCameraInfos.size() - 1).getEND_TIME());
groupHash.put(audioCameraInfos.get(audioCameraInfos.size() - 1).getEND_TIME(), withSameIpCameraInfos);
}
if (i != 0 && i != (operateCameraInfoList.size() - 1)) {
String curOpCameraIp = operateCameraInfoList.get(i).getCAMERA_IP();
String preOpCameraIp = operateCameraInfoList.get(i - 1).getCAMERA_IP();
if (StringUtils.isNotBlank(curOpCameraIp) && StringUtils.isNotBlank(preOpCameraIp)) {
if (curOpCameraIp.equals(preOpCameraIp)) {
withSameIpCameraInfos.add(operateCameraInfoList.get(i));
} else {
endTimeList.add(withSameIpCameraInfos.get(withSameIpCameraInfos.size() - 1).getEND_TIME());
groupHash.put(withSameIpCameraInfos.get(withSameIpCameraInfos.size() - 1).getEND_TIME(), withSameIpCameraInfos);
withSameIpCameraInfos = new ArrayList<>();
withSameIpCameraInfos.add(operateCameraInfoList.get(i));
}
}
}
}
其中这段代码实在一个Thread线程里操作的,上面的代码我对endTimeList集合进行操作所以报了这个错误,解决的方法是我使用了CopyOnWriteArrayList如下:
List<String> endTimeList = new CopyOnWriteArrayList<>();
========================2018年6月15日更新===========================
今天执行以下代码又报这个错误,
private List<Map> getShowFieldNames(String tableName) {
List<Map> fieldName = new ArrayList<>();
//根据ds_id没有查询到要展示的字段信息,则展示该表的所有字段
fieldName = queryInfoMapper.listAllTableFiledName(tableName);
if (fieldName.size() > 0) {
for (Map map : fieldName) {
if ("PID".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_USER_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_USER_NAME".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_TIME".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_USER_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_USER_NAME".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_TIME".equalsIgnoreCase((String) map.get("FIELD"))
|| "REMARK".equalsIgnoreCase((String) map.get("FIELD"))
|| "DRRW_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "APPLY_DS_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "APPLY_OBJECT_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "INIT_PID".equalsIgnoreCase((String) map.get("FIELD"))) {
fieldName.remove(map);
}
}
}
return fieldName;
}
这次参考了另一种解决方式,使用removeAll方法:
private List<Map> getShowFieldNames(String tableName) {
List<Map> fieldName = new ArrayList<>();
List<Map> tempField = new ArrayList<>();
//根据ds_id没有查询到要展示的字段信息,则展示该表的所有字段
fieldName = queryInfoMapper.listAllTableFiledName(tableName);
if (fieldName.size() > 0) {
for (Map map : fieldName) {
if ("PID".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_USER_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_USER_NAME".equalsIgnoreCase((String) map.get("FIELD"))
|| "CRT_TIME".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_USER_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_USER_NAME".equalsIgnoreCase((String) map.get("FIELD"))
|| "UPDATE_TIME".equalsIgnoreCase((String) map.get("FIELD"))
|| "REMARK".equalsIgnoreCase((String) map.get("FIELD"))
|| "DRRW_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "APPLY_DS_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "APPLY_OBJECT_ID".equalsIgnoreCase((String) map.get("FIELD"))
|| "INIT_PID".equalsIgnoreCase((String) map.get("FIELD"))) {
tempField.add(map);
}
}
fieldName.removeAll(tempField);
}
return fieldName;
}
参考以下文章:
- http://www.cnblogs.com/dolphin0520/p/3933551.html;
- http://www.jianshu.com/p/c5b52927a61a;
- http://blog.csdn.net/mazhimazh/article/details/19210547;
- java.util.ConcurrentModificationException 出现的原因和解决办法;
最新评论
我的是ipv4网络,如何使用直播源啊!
我今天试了,不想啊,我的是新疆昌吉移动的网络。
收不到验证码电报
现在充值29起了