user16722881
user16722881

Reputation:

Why doesn't ConcurrentModificationException happen with remove method of LinkedList?

Deque<Employee> employeeDeque = new LinkedList<>();
        employeeDeque.offerLast(new Employee("Michael", 250));
        employeeDeque.offerLast(new Employee("John", 250));
            Iterator iterator = employeeDeque.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            employeeDeque.remove(new Employee("Michael", 250));
    }

The same code with ArrayList with produce this exception, but remove on LinkedList doesn't. Why is it so? Add() and Offer() methods still produce it.

Upvotes: 0

Views: 38

Answers (1)

Solomon Slow
Solomon Slow

Reputation: 27190

The Javadoc for LinkedList says,

Note that the fail-fast behavior of an iterator cannot be guaranteed...Fail-fast iterators throw ConcurrentModificationException on a best-effort basis.

Upvotes: 1

Related Questions