WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Expert · question 79 of 100

How does the ConcurrentModificationException occur in Java Collections, and how do you prevent it?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

The ConcurrentModificationException is a runtime exception that is thrown by Java Collections when an object is modified in a way that is not supported by the underlying collection. This can occur when an iterator is used to traverse a collection and an attempt is made to modify the collection directly, or when multiple threads attempt to modify the collection concurrently.

The ConcurrentModificationException occurs when an iterator is used to traverse a collection, and the collection is modified during the traversal. This can happen if an element is added, removed or modified in the collection while an iterator is still in progress. When this occurs, the iterator detects the modification and throws the ConcurrentModificationException.

To prevent the ConcurrentModificationException, one approach is to use a CopyOnWriteArrayList or ConcurrentHashMap, which are designed to be modified concurrently. Another approach is to use the Iterator interface’s remove() method, which removes the current element from the collection and prevents the ConcurrentModificationException from being thrown. However, it is important to note that calling remove() multiple times for the same element can result in unexpected behavior.

Here is an example of how the ConcurrentModificationException can occur and how to prevent it:

// Create a list of integers
List<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

// Create an iterator for the list
Iterator<Integer> iterator = myList.iterator();

// Traverse the list using the iterator
while (iterator.hasNext()) {
    Integer element = iterator.next();
    System.out.println(element);

    // Add a new element to the list during the traversal
    myList.add(6); // Throws ConcurrentModificationException
}

In this example, a ConcurrentModificationException is thrown because the list is modified during the traversal. To prevent this, we can modify the code as follows:

List<Integer> myList = new CopyOnWriteArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

Iterator<Integer> iterator = myList.iterator();

while (iterator.hasNext()) {
    Integer element = iterator.next();
    System.out.println(element);

    // Add a new element to the list during the traversal
    myList.add(6); // Does not throw ConcurrentModificationException
}

In this modified example, a CopyOnWriteArrayList is used instead of an ArrayList, and the ConcurrentModificationException is prevented because the CopyOnWriteArrayList is designed to be modified concurrently.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics