How to do it...

Each of the concurrent collections of the java.util.concurrent package implements (or extends, if it is an interface) one of the four interfaces of the java.util package: List, Set, Map, or Queue:

  1. The List interface has only one implementation: the CopyOnWriteArrayList class. The following is taken from the Javadoc of this class:
"all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array... The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.