0

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.

Aksanth
  • 269
  • 2
  • 13
  • You can not remote an item from a collection while you are iterating on it, except if the `Iterator` implements the `remove(...)` method. But I do not understand while you are getting an error on a HashMap while you are using a Set ? – zer0chain Apr 02 '16 at 16:51
  • Possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – fgb Apr 02 '16 at 16:51

0 Answers0