CopyOnWriteArrayList

public class CopyOnWriteArrayList extends Object
implements List<E> RandomAccess Cloneable Serializable

A thread-safe random-access list.

Read operations (including get(int)) do not block and may overlap with update operations. Reads reflect the results of the most recently completed operations. Aggregate operations like addAll(int, Collection) and clear() are atomic; they never expose an intermediate state.

Iterators of this list never throw ConcurrentModificationException. When an iterator is created, it keeps a copy of the list's contents. It is always safe to iterate this list, but iterations may not reflect the latest state of the list.

Iterators returned by this list and its sub lists cannot modify the underlying list. In particular, remove(), add(E) and set(E) all throw UnsupportedOperationException.

This class offers extended API beyond the List interface. It includes additional overloads for indexed search (indexOf(E, int) and lastIndexOf(E, int)) and methods for conditional adds (addIfAbsent(E) and addAllAbsent(Collection)).

Public Constructor Summary

CopyOnWriteArrayList()
Creates a new empty instance.
CopyOnWriteArrayList(Collection<? extends E> collection)
Creates a new instance containing the elements of collection.
CopyOnWriteArrayList(E[] array)
Creates a new instance containing the elements of array.

Public Method Summary

synchronized boolean
add(E e)
Appends the specified element to the end of this list (optional operation).
synchronized void
add(int index, E e)
Inserts the specified element at the specified position in this list (optional operation).
synchronized boolean
addAll(Collection<? extends E> collection)
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 (optional operation).
synchronized boolean
addAll(int index, Collection<? extends E> collection)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
synchronized int
addAllAbsent(Collection<? extends E> collection)
Adds the elements of collection that are not already present in this list.
synchronized boolean
addIfAbsent(E object)
Adds object to the end of this list if it is not already present.
synchronized void
clear()
Removes all of the elements from this list (optional operation).
Object
clone()
Creates and returns a copy of this Object.
boolean
contains(Object o)
Returns true if this list contains the specified element.
boolean
containsAll(Collection<?> collection)
Returns true if this list contains all of the elements of the specified collection.
boolean
equals(Object other)
Compares this instance with the specified object and indicates if they are equal.
void
forEach(Consumer<? super E> action)
E
get(int index)
Returns the element at the specified position in this list.
int
hashCode()
Returns an integer hash code for this object.
int
indexOf(E object, int from)
Searches this list for object and returns the index of the first occurrence that is at or after from.
int
indexOf(Object object)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
boolean
isEmpty()
Returns true if this list contains no elements.
Iterator<E>
iterator()
Returns an Iterator that iterates over the elements of this list as they were at the time of this method call.
int
lastIndexOf(Object object)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
int
lastIndexOf(E object, int to)
Searches this list for object and returns the index of the last occurrence that is before to.
ListIterator<E>
listIterator(int index)
Returns a ListIterator that iterates over the elements of this list as they were at the time of this method call.
ListIterator<E>
listIterator()
Equivalent to listIterator(0).
synchronized E
remove(int index)
Removes the element at the specified position in this list (optional operation).
synchronized boolean
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation).
synchronized boolean
removeAll(Collection<?> collection)
Removes from this list all of its elements that are contained in the specified collection (optional operation).
synchronized void
replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element.
synchronized boolean
retainAll(Collection<?> collection)
Retains only the elements in this list that are contained in the specified collection (optional operation).
synchronized E
set(int index, E e)
Replaces the element at the specified position in this list with the specified element (optional operation).
int
size()
Returns the number of elements in this list.
synchronized void
sort(Comparator<? super E> c)
Sorts this list using the supplied Comparator to compare elements.
List<E>
subList(int from, int to)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[]