这块代码remove有问题,帮我改一下private void addSpcLogs(List<SpcLogs> spcLogs,String refId,Integer refType,Integer refOption, UserInfoDTO userInfoDTO, LocalDateTime effectiveDate) { List<SpcRoleUserScope> spcRoleUserScopeList = userScopeMapper.listByRefIdAndTypeAndOption(refId, refType, refOption); if (CollectionUtils.isNotEmpty(spcLogs)){ for (SpcRoleUserScope spcRoleUserScope : spcRoleUserScopeList) { for (SpcLogs spcLog : spcLogs) { if (spcRoleUserScope.getRefId().equals(spcLog.getGrantNumber())){ spcRoleUserScopeList.remove(spcRoleUserScope); } } } }}
时间: 2024-02-15 21:27:58 浏览: 69
这段代码的问题在于,在遍历spcRoleUserScopeList列表的同时,修改了列表的元素,这会导致ConcurrentModificationException异常。为了解决这个问题,可以使用Iterator迭代器来遍历列表并删除元素,如下所示:
private void addSpcLogs(List<SpcLogs> spcLogs, String refId, Integer refType, Integer refOption, UserInfoDTO userInfoDTO, LocalDateTime effectiveDate) {
List<SpcRoleUserScope> spcRoleUserScopeList = userScopeMapper.listByRefIdAndTypeAndOption(refId, refType, refOption);
if (CollectionUtils.isNotEmpty(spcLogs)) {
Iterator<SpcRoleUserScope> it = spcRoleUserScopeList.iterator();
while (it.hasNext()) {
SpcRoleUserScope spcRoleUserScope = it.next();
for (SpcLogs spcLog : spcLogs) {
if (spcRoleUserScope.getRefId().equals(spcLog.getGrantNumber())) {
it.remove();
break;
}
}
}
}
}
这样,就可以安全地遍历并修改spcRoleUserScopeList列表的元素了。
阅读全文