Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know if would be trying to remove from collection looping through it with the simple loop I will be getting this exception: java.util.ConcurrentModificationException. But I am using Iterator and it still generates me this exception. Any idea why and how to solve it?

HashSet<TableRecord> tableRecords = new HashSet<>();

...

    for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) {
        TableRecord record = iterator.next();
        if (record.getDependency() == null) {
            for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) {
                TableRecord dependency = dependencyIt.next(); //Here is the line which throws this exception
                if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) {
                    iterator.remove();
                }
            }
        }
    }
share|improve this question

3 Answers 3

up vote 4 down vote accepted

You must use iterator.remove() instead of tableRecords.remove()

You can remove items on a list on which you iterate only if you use the remove method from the iterator.

EDIT :

When you create an iterator, it starts to count the modifications that were applied on the collection. If the iterator detects that some modifications were made without using its method (or using another iterator on the same collection), it cannot guarantee anymore that it will not pass twice on the same element or skip one, so it throws this exception

It means that you need to change your code so that you only remove items via iterator.remove (and with only one iterator)

OR

make a list of items to remove then remove them after you finished to iterate.

share|improve this answer
    
There are two nested iterators so it will probably not solve the issue. –  assylias Jun 6 '13 at 15:02
1  
Still the same. –  user2219247 Jun 6 '13 at 15:04
    
@assylias That's true, I did not see the second one. I added some explanation on this exception. –  Arnaud Denoyelle Jun 6 '13 at 15:10
    
Second option of making a list of items to remove solved my issue. Thanks. –  user2219247 Jun 6 '13 at 15:12

The contract for HashSet's iterator is that you can't remove from the hashset other than through that spcific iterator's remove method. From the perspective of dependencyIt, you have removed an item other than by calling its remove method so it throws a ConcurrentModificationException.

It seems you want to remove records from your hashset when they have the same record id. Wouldn't it be easier to override your records's equals and hashcode methods to ensure that records with the same id equal and have the same hashcode? (if that makes sense of course)

share|improve this answer

The issue is that you have two iterators in scope at the same time and they're "fighting" with each other. The easiest way to fix the issue is just to bail out of the inner loop if you find a match:

for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) {
    TableRecord record = iterator.next();
    if (record.getDependency() == null) {
        for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) {
            TableRecord dependency = dependencyIt.next(); //Here is the line which throws this exception
            if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) {
                iterator.remove();
                break; // ADD THIS LINE
            }
        }
    }
}

Java Iterators are intended to "fail fast" whenever their underlying container is changed without having been changed using the Iterator. You're using nested iterators, so any remove() operation issued to one will cause the other to throw an Exception if it continues to be used. For this reason, if you need to issue a remove(), then you'll need to do it on the "outer" iterator (which you're doing) and discontinue use of the second iterator afterwards (which the added break statement does).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.