0

I wanted to modify a list while iterating over it but realized that's not a good practice, so I had to use the iterator, but I'm kind of surprised that modifying iterator change the underlying list..

I want to use item through out the program but modify it if it contains a specific value. How does the iterator do this actually? I mean item is not being modified, the iterator object called domainCDCIter is the one that we call .remove() on... How would that cause item to be modified?

Could someone demystify this?

    Iterator<DomainCDC> domainCDCIter = item.getChangeList().iterator();
    while (domainCDCIter.hasNext())
    {
        // if record is populated with DELETE, we want to remove it from changeList.
        final DomainCDC domainCDC = domainCDCIter.next();
        if(StringUtils.isNotEmpty(domainCDC.getEvent()) && domainCDC.getEvent().equals("DELETE")){
            deleteFromSearchList.add(domainCDC);
            deletableDomainCDCStatusMap.put(domainCDC.getObjectId(), domainCDC.getDomainCDCStatus());
            domainCDCIter.remove();
        } else{
            indexableDomainCDCStatusMap.put(domainCDC.getObjectId(), domainCDC.getDomainCDCStatus());
        }
    }

thanks in advance...

user836087
  • 2,271
  • 8
  • 23
  • 33

0 Answers0