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

Time:10-27

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.

CodePudding user response:

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.

  •  Tags:  
  • java
  • Related