I am trying to remove a object from Set, and i am using Iterator. Still getting concurrentmodificationexception in the second iteration for the line JunOffer affoffer = iterator.next();.
List<sWrapper> sWrapperList = new ArrayList<sWrapper>();
for (Offering offering : offerings) {
sWrapper wrapper = new sWrapper(offering);
Set<JunOffer> junOfferSet = wrapper.getOffering().getJunOffers();
if (junOfferSet != null) {
for (Iterator<JunOffer> iterator = junOfferSet.iterator(); iterator.hasNext(); ) {
JunOffer affoffer = iterator.next();
if (affoffer.getOfferingType() == null ) {
junOfferSet.remove(affoffer);
}
}
}
sWrapperList.add(wrapper);
}
Exception:
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:841)
at java.util.HashMap$KeyIterator.next(HashMap.java:877)
at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:555)
at com.myself.me.off.services.SearchService.findAndValidateOffer(SearchServiceImpl.java:2536)
Please help to fix it.
Thanks in advance.