V.S
V.S

Reputation: 165

Behavior of concurrentmodification exception ? how iterator internally works.Why exception is not thrown in second case

Behavior of concurrentmodification exception ? how iterator internally works for ArrayList.Why exception is not thrown in second case.? and why control is coming inside loop in first case though iterator has already covered all elements of collection.

    //Case First
    /**********Gives Concurrent Modification Exception ****/////////////
    public static void main(String[] args) {
            List<String> sampleList = createSampleList();
            int i = 0;
            for (Iterator iterator = sampleList.iterator(); iterator.hasNext();) {
                i++;
                System.out.println("Value of I "+i);
                String value = (String) iterator.next();
                if(i==sampleList.size()){
                    System.out.println("Removing element");
                sampleList.remove(value);
                }
                System.out.println(value);
            }
        }
        private static List<String> createSampleList(){
            List<String> sampleList = new ArrayList<String>();
            sampleList.add("one");
            sampleList.add("two");
            sampleList.add("three");
            sampleList.add("four");
            sampleList.add("five");
            return sampleList;
        }
/**Output**/
Value of I 1
one
Value of I 2
two
Value of I 3
three
Value of I 4
four
Value of I 5
Removing element
five
Value of I 6
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at com.collection.iterator.Test.main(Test.java:17)


/**Output**/
   //Second Case 
/**********Does not Give Concurrent Modification *******/////////////

    public static void main(String[] args) {
            List<String> sampleList = createSampleList();
            int i = 0;
            for (Iterator iterator = sampleList.iterator(); iterator.hasNext();) {
                i++;
                System.out.println("Value of I "+i);
                String value = (String) iterator.next();
                if(i==sampleList.size()-1){
                    System.out.println("Removing element");
                sampleList.remove(value);
                }
                System.out.println(value);
            }
        }
        private static List<String> createSampleList(){
            List<String> sampleList = new ArrayList<String>();
            sampleList.add("one");
            sampleList.add("two");
            sampleList.add("three");
            sampleList.add("four");
            sampleList.add("five");
            return sampleList;
        }
/**Output**/
Value of I 1
one
Value of I 2
two
Value of I 3
three
Value of I 4
Removing element
four
/**Output**/

Upvotes: 0

Views: 60

Answers (1)

V.S
V.S

Reputation: 165

I got the answer. Actually answer lies in implementation of iterator in ArrayList.

First Case://Everyone knows why it is throwing exception because we are changing the structure of collection other than iterator.remove method. but question was : Though it has printed all the five elements,it is again coming in the loop and throwing exception. Below is the implementation of hasNext() method of iterator.So it is checking the cursor(iterator maintains a cursor while iterating the elements) with size of arraylist. SO in first case after reaching fifth element,cursor became 5 and we have removed one element from arraylist so size became for 4.So when in the loop,hasnext() method got called,it returns true and enters into the loop.

public boolean hasNext() {
    return (this.cursor != ArrayList.this.size);
}

This also explains Why exception is not thrown in second case because when we have removed 4th element cursor value is 4 and ArrayList size also became 4.So has next return false in that case and it does not come into loop and does not next() method which actually throws ConcurrentModificationException.

Upvotes: 1

Related Questions