LinkedList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop in java



LinkedList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop.


iterator returned by LinkedList is fail-fast. Means any structural modification made to LinkedList like adding or removing elements during Iteration will throw java.util.ConcurrentModificationException.

import java.util.LinkedList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class LinkedListFailFastExample {
   public static void main(String args[]) {
      List<String> linkedList = new LinkedList<String>();
      linkedList.add("ankit");
      linkedList.add("javaMadeSoEasy");
      System.out.println("-------use iterator-------");
          // fail-fast
      Iterator<String> iterator = linkedList.iterator();
      while (iterator.hasNext()) {
             linkedList.add("newEle"); // unComment to avoid ConcurrentModificationException
             System.out.println(iterator.next());
      }
      System.out.println("-------use listIterator-------");
      // fail-fast
      ListIterator<String> listIterator = linkedList.listIterator();
      listIterator.next();
      listIterator.previous();
      while (listIterator.hasNext()) {
             linkedList.add("newEle"); // unComment to avoid ConcurrentModificationException
             System.out.println(listIterator.next());
      }
      System.out.println("-------use Enumeration-------");
      // fail-fast
      Enumeration<String> listEnum = Collections.enumeration(linkedList);
      while (listEnum.hasMoreElements()) {
             linkedList.add("newEle"); // unComment to avoid ConcurrentModificationException
             System.out.println(listEnum.nextElement());
      }
      System.out.println("-------use enhanced for loop-------");
          //enhanced for loop is fail-fast
     for (String string : linkedList) {
             linkedList.add("newEle"); // unComment to avoid ConcurrentModificationException
             System.out.println(string);
      }
   }
}
/*OUTPUT
-------use iterator-------
Exception in thread "main" java.util.ConcurrentModificationException
   at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
   at java.util.LinkedList$ListItr.next(Unknown Source)
   at LinkedListFailFastExample .main(LinkedListExample.java:26)
*/



RELATED LINKS>

ArrayList - add, add element at specific index methods program


ArrayList - remove, get, contains and set methods program


ArrayList - iterate using iterator, listIterator, Enumeration and enhanced for loop


ArrayList - isEmpty, size and clear methods program


ArrayList - synchronizing using Collections.synchronizedList


ArrayList - making list unmodifiable using Collections.unmodifiableList


Vector - add, add element at specific index methods program


No comments:

Post a Comment