-4

I am getting a ConcurrentModificationException with my for loop and I do not know how to solve this problem.

This is my code:

for(Map.Entry<String,ControlFieldVO> entry:oldPresentationMap.entrySet()) {
                                    if(!keyValue.contains(entry.getKey())) {
                                        oldPresentationMap.remove(entry.getKey());
}

Getting concurrentModification exception at first line please help and below are the stack trace:

<<<[2014/05/24 17:29:03.595]:WebContainer : 10:1400932743595:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:isParameterNotNull::Paremeter :nullControlFieldVO :com.td.mbnasales.core.jackrabbit.vo.PresentationImageVO@5f415f41[sameParentImgName=<null>,imgName=<null>,value=,isParentValueOverwritten=false,isErrorInValue=<null>,isValueDiffFromPublishedVersion=false,sameParentValue=,publishedValue=<null>,isMultiEditFieldValueDiff=false,isChanged=<null>]  >>>
<<<[2014/05/24 17:29:03.595]:WebContainer : 10:1400932743595:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:isParameterNotNull::Paremeter :nullControlFieldVO :com.td.mbnasales.core.jackrabbit.vo.PresentationImageVO@5f205f20[sameParentImgName=<null>,imgName=<null>,value=<null>,isParentValueOverwritten=false,isErrorInValue=<null>,isValueDiffFromPublishedVersion=false,sameParentValue=<null>,publishedValue=<null>,isMultiEditFieldValueDiff=<null>,isChanged=<null>]  >>>
<<<[2014/05/24 17:29:03.600]:WebContainer : 10:1400932743600:java.lang.String:    ERROR:c7WtHlNAp7cfpveDNFSG8Iq:resolveException::Exception Occured:java.util.ConcurrentModificationException  >>>
<<<[2014/05/24 17:29:03.603]:WebContainer : 10:1400932743603:java.lang.String:    ERROR:c7WtHlNAp7cfpveDNFSG8Iq:resolveException::Stack Trace :java.util.ConcurrentModificationException
    at java.util.LinkedHashMap$AbstractMapIterator.checkConcurrentMod(Unknown Source)
    at java.util.LinkedHashMap$AbstractMapIterator.makeNext(Unknown Source)
    at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
    at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
    at com.td.mbnasales.wwwcms.controller.helper.EntityHelper.mergePresentationSessionMultiEntityVO(EntityHelper.java:5849)
    at com.td.mbnasales.wwwcms.controller.helper.EntityHelper.multiUpdateSessionEntityVO(EntityHelper.java:6010)
    at com.td.mbnasales.wwwcms.controller.EntityController.multiUpdateSessionEntityVO(EntityController.java:5999)
    at sun.reflect.GeneratedMethodAccessor9348.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:599)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1597)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:131)
    at com.td.mbnasales.wwwcms.filters.AccessControlFilter.doFilter(AccessControlFilter.java:77)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.td.mbnasales.wwwcms.filters.BreadCrumbFilter.doFilter(BreadCrumbFilter.java:336)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.td.mbnasales.wwwcms.filters.LocaleFilter.doFilterInternal(LocaleFilter.java:110)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:934)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
    at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1613)
  >>>
<<<[2014/05/24 17:29:25.392]:WebContainer : 10:1400932765392:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:validateLocalParam -->::called  >>>
<<<[2014/05/24 17:29:25.393]:WebContainer : 10:1400932765393:com.td.mbnasales.core.helper.BreadCrumbHelper:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getBreadCrumbDetails::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.393]:WebContainer : 10:1400932765393:com.td.mbnasales.core.helper.BreadCrumbHelper:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getBreadCrumbDetails:: --- Exit ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:com.td.mbnasales.core.service.impl.MarketingZoneServiceImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getContent::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:get::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.485]:WebContainer : 10:1400932765485:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.486]:WebContainer : 10:1400932765486:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:485 to 17:29:25:486 1 ms  Taken for getOCMnullnull
  >>>
<<<[2014/05/24 17:29:25.744]:WebContainer : 10:1400932765744:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq:CECOM:TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:485 to 17:29:25:743 258 ms  Taken for get(ContentItem contentItem,ContentItemAttribute contentItemAttribute)null - CECOM
  >>>
<<<[2014/05/24 17:29:25.744]:WebContainer : 10:1400932765744:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:get:: --- Exit ---   >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:getPublishedVersion::---- Enter ---   >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.745]:WebContainer : 10:1400932765745:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 17:29:25:745 to 17:29:25:745 0 ms  Taken for getOCMnullnull
  >>>
<<<[2014/05/24 17:29:25.948]:WebContainer : 10:1400932765948:java.lang.String:    DEBUG:c7WtHlNAp7cfpveDNFSG8Iq:New JackRabbit Session created for the user::JackRabbitManagerImpl  >>>
<<<[2014/05/24 17:29:25.949]:WebContainer : 10:1400932765949:com.td.mbnasales.core.jcr.manager.impl.JackRabbitManagerImpl:     PERF:c7WtHlNAp7cfpveDNFSG8Iq::TIME:<TIMESTAMP> (0) 2014/05/24 1

Can anyone help me?

Kets
  • 438
  • 2
  • 8
  • 24
user3671657
  • 37
  • 1
  • 8
  • 2
    What does the message say? "Concurrent modification." What does that mean? You're iterating on a collection object and modifying that same object (adding/removing elements) inside the loop. Don't do that! – Hot Licks May 24 '14 at 12:18

2 Answers2

2

You get ConcurrentModificationException if you try to modify the map while iterating. You can circumvent this by:

  • store elements to be removed in a list, set or other separate data structure and remove them after loop
  • use the safe remove semantics provided by Iterator
kviiri
  • 3,282
  • 1
  • 21
  • 30
0

You have obtained an iterator on the key set of the map and are structurally modifying the map directly. The behavior for this type of action is defined by the map implementation. If you look at the javadoc for hash map:

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

It describes the behavior you are seeing.

Instead of using the for:next syntax, do this the old way of using an Iterator and call Iterator.remove to remove the item.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25