java - ConcurrentModificationException and a HashMap -


i using persisting objects using jpa. main object has owning one-many relationship object. other object stored in hashmap. sort of synchronisation fix problem? seems happen @ random times , unpredictable. here exception get:

exception in thread "pool-1-thread-1" java.util.concurrentmodificationexception         @ java.util.hashmap$hashiterator.nextentry(unknown source)         @ java.util.hashmap$valueiterator.next(unknown source)         @ org.hibernate.collection.abstractpersistentcollection$iteratorproxy.next(abstractpersistentcollection.java:555)         @ org.hibernate.engine.cascade.cascadecollectionelements(cascade.java:296)         @ org.hibernate.engine.cascade.cascadecollection(cascade.java:242)         @ org.hibernate.engine.cascade.cascadeassociation(cascade.java:219)         @ org.hibernate.engine.cascade.cascadeproperty(cascade.java:169)         @ org.hibernate.engine.cascade.cascade(cascade.java:130) 

this not synchronization problem. occur if underlying collection being iterated on modified other iterator itself.

iterator = map.entryset().iterator(); while (it.hasnext()) {    entry item = it.next();    map.remove(item.getkey()); } 

this throw concurrentmodificationexception when it.hasnext() called second time.

the correct approach

   iterator = map.entryset().iterator();    while (it.hasnext())    {       entry item = it.next();       it.remove();    } 

assuming iterator supports remove() operation.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -