Exception in thread "main" java.lang.IllegalStateException during Iterator.remove()

Posted by Somesh Shinde On Sunday, 26 June 2016 0 comments

Exception in thread "main" java.lang.IllegalStateException during Iterator.remove()


I was writing a sample program to demonstate how to remove elments from list while iterating over it by using Iterator.remove() mehtod. Unfortunately I was getting following error, right at the place where I was calling the Iterator.remove() method :

Exception in thread "main" java.lang.IllegalStateException
 at java.util.ArrayList$Itr.remove(Unknown Source)
 at dto.IteratorRemoveTest.main(IteratorRemoveTest.java:25)

I was puzzled, why I am getting this error because I was using Iterator's remove() method and not the ArrayList's remove() method, which causes ConcurrentModificationException in Java.


java.lang.IllegalStateException during Iterator.remove()

Here is my code, Can you spot what is wrong, without looking at the solution
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Java Program to demonstrate how to remove elements from List while
 * traversing over it using Iterator.
 * 
 * @author WINDOWS 8
 */
public class IteratorRemoveTest {

    public static void main(String args[]) {

        List<String> brands = new ArrayList(
                                Arrays.asList("Apple", "Microsoft, Google"));     
        
        System.out.println("Java Program to remove elements from list via Iteator");
        System.out.println("List before removing elements: " + brands);
        
        for (final Iterator<String> itr = brands.iterator(); itr.hasNext();) {
            itr.remove();
        }
        
        System.out.println("List after removing elements: " + brands);
    }

}

Output:
Java Program to remove elements from list using Iteator
List before removing elements: [Apple, Microsoft, Google]
Exception in thread "main" java.lang.IllegalStateException
    at java.util.ArrayList$Itr.remove(ArrayList.java:849)
    at IteratorRemoveTest.main(IteratorRemoveTest.java:25)
Java Result: 1


By carefully looking at code and error message I later figure out that I was calling Iterator.remove() method without calling the Iterator.next(), which was causing this probelm.

To fix this just call the Iterator.next() before calling Iterator.remove() method, as shown below:
for (final Iterator<String> itr = brands.iterator(); itr.hasNext();) {
     itr.next();
     itr.remove(); 
}

Now, if you run the Java program, you will get following output
Java Program to remove elements from list using Iteator
List before removing elements: [Apple, Microsoft, Google]
List after removing elements: []

Our code ran successfully.  Here is the nice summary of things you should know while trying to delete elments from Java collection or list using Iteartor's remove() method:

Exception in thread "main" java.lang.IllegalStateException during Iterator.remove()

That's all about how to solve Exception in thread "main" java.lang.IllegalStateException during Iterator.remove() in Java program. Sometime, you make silly mistakes which causes error looking like a big monster. So, if you can't see the error, take a break and they look it again, you will find it. 

0 comments:

Post a Comment