【Java】java.util.ConcurrentModificationException
异常提示信息:
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:742)
at com.yonyou.cloud.repair.service.dcs.part.partmaindataimport.PartMainDataImportServiceImpl.lambda$getMainDataList2$5(PartMainDataImportServiceImpl.java:282)
at java.util.stream.ReferencePipeline$11$1.accept(ReferencePipeline.java:372)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.yonyou.cloud.repair.service.dcs.part.partmaindataimport.PartMainDataImportServiceImpl.getMainDataList2(PartMainDataImportServiceImpl.java:293)
at com.yonyou.cloud.repair.service.dcs.part.partmaindataimport.PartMainDataImportServiceImpl.importUpload(PartMainDataImportServiceImpl.java:541)
at com.yonyou.cloud.repair.service.dcs.part.partmaindataimport.PartMainDataImportServiceImpl$$FastClassBySpringCGLIB$$a14684e1.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.yonyou.cloud.repair.service.dcs.part.partmaindataimport.PartMainDataImportServiceImpl$$EnhancerBySpringCGLIB$$2047ea2c.importUpload(<generated>)
at com.yonyou.cloud.repair.controller.dcs.part.partmaindataimport.PartMainDataImportController.importUpload(PartMainDataImportController.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
问题代码:
for (String s : row.keySet()) { if (DICT_1004_FIELDS.contains(s)) row.computeIfPresent(s, (key, value) -> YES_NO_FIELDS.get(0).equals(value) ? YES : NO); // 删除空值 Key if (StringUtils.isNullOrEmpty(row.get(s))) row.remove(s); }
这里做了一个删除操作,使用For遍历或者 调用forEach函数都会出现
这种遍历方式不允许元素的个数调整
需要更改使用迭代器实现:
// 删除空Val的Key row.entrySet().removeIf(next -> StringUtils.isNullOrEmpty(next.getValue()));
或者改用stream流操作来实现:
return ownPriceList.stream().map(m -> { // 翻译删除 m.computeIfPresent(IS_DELETED, (key, value) -> YES_NO_FIELDS.get(2).equals(value) ? 1 : 0); // 删除缺省设置 if (StringUtils.isNullOrEmpty(m.get(IS_DELETED))) m.put(IS_DELETED, 0); // 结束时间更新校验 Object endAt = m.get(END_AT); if (m.get(IS_DELETED).equals(1) && StringUtils.isNullOrEmpty(endAt) && 0 < now.compareTo(DateUtil.parse(endAt.toString()))) { m.remove(END_AT); } return m; }).collect(Collectors.toList());