java-concurrentmodification-exception

Do you have these questions?

  • Java – how to avoid ConcurrentModificationException
  • concurrentmodificationexception iterator next
  • concurrent modification exception in java hashmap

In previous topic we learn how to iterate through Java Collection class ArrayList. Also used best practice to avoid possible Out Of Memory (OOM) situation.

In this tutorial we will see some basic fundamentals of Java Iterator and possible ConcurrentModificationException.

Introduced in the Java JDK 1.2 release, the java.util.Iterator interface allows the iteration of Collection classes. Each Iterator provides a next() and hasNext() method, and may optionally support a remove() method. Iterators are created by the corresponding container class, typically by a method named iterator(). It is an improvement over Enumeration interface.

Iterator Methods

Iterator defines three methods, one of which is optional.

Result

Method

Description

b = it.hasNext() true if there are more elements for the iterator.
obj = it.next() Returns the next object. If a generic list is being accessed, the iterator will return something of the list’s type. Pre-generic Java iterators always returned type Object, so a downcast was usually required.
  it.remove() Removes the most recent element that was returned by next. Not all collections support delete. An UnsupportedOperationException will be thrown if the collection does not support remove().

Important points:

  • Iterators are unidirectional. We can iterate only in one direction.
  • Iteration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new Iterator.

Difference between Iterator and Enumeration interfaces

  1. remove() method is introduced in iterator. Using this method we can remove element from the underlying collection which we are iterating.
  2. Enumeration has two methods and both are available in iterator. Method names for both of them are shortened.

ListIterator methods

ListIterator is implemented only by the classes that implement the List interface (ArrayListLinkedList, and Vector). ListIterator provides the following.

Result

Method

Description

Forward iteration
b = it.hasNext() true if there is a next element in the collection.
obj = it.next() Returns the next object.
Backward iteration
b = it.hasPrevious() true if there is a previous element.
obj = it.previous() Returns the previous element.
Getting the index of an element
i = it.nextIndex() Returns index of element that would be returned by subsequent call to next().
i = it.previousIndex() Returns index of element that would be returned by subsequent call to previous().
Optional modification methods. UnsupportedOperationException thrown if unsupported.
  it.add(obj) Inserts obj in collection before the next element to be returned by next() and after an element that would be returned by previous().
  it.set() Replaces the most recent element that was returned by next or previous().
  it.remove() Removes the most recent element that was returned by next() or previous().

ConcurrentModificationException Basic

We cannot add or remove elements to the underlying collection when we are using an iterator.

package crunchify.com.tutorial;

import java.util.ArrayList;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyJavaIteratorConcurrentModificationException {

        public static void main(String[] args) {
                ArrayList<String> company = new ArrayList();
                company.add("eBay");
                company.add("Paypal");
                company.add("Google");

                for (String s : company) {
                        System.out.println(s);
                        company.add("Yahoo");
                }
        }
}

Eclipse Console Output:

java.util.ConcurrentModificationException Example

The post Java Iterator, ListIterator fundamentals and ConcurrentModification Exception appeared first on Crunchify.