Module java.base

Class CopyOnWriteArrayList<E>

  • Type Parameters:
    E - the type of elements held in this list
    All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess


    public class CopyOnWriteArrayList<E>
    extends Object
    implements List<E>, RandomAccess, Cloneable, Serializable
    A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

    This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads. 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. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

    All elements are permitted, including null.

    Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread.

    This class is a member of the Java Collections Framework.

    Since:
    1.5
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      CopyOnWriteArrayList​()
      Creates an empty list.
      CopyOnWriteArrayList​(E[] toCopyIn)
      Creates a list holding a copy of the given array.
      CopyOnWriteArrayList​(Collection<? extends E> c)
      Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E element)
      Inserts the specified element at the specified position in this list.
      boolean add​(E e)
      Appends the specified element to the end of this list.
      boolean addAll​(int index, Collection<? extends E> c)
      Inserts all of the elements in the specified collection into this list, starting at the specified position.
      boolean addAll​(Collection<? extends E> c)
      Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
      int addAllAbsent​(Collection<? extends E> c)
      Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.
      boolean addIfAbsent​(E e)
      Appends the element, if not present.
      void clear​()
      Removes all of the elements from this list.
      Object clone​()
      Returns a shallow copy of this list.
      boolean contains​(Object o)
      Returns true if this list contains the specified element.
      boolean containsAll​(