java.util.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.
Modifier and Type | Class and Description |
---|---|
pack-priv static class | |
private class | CopyOnWriteArrayList.
Sublist for CopyOnWriteArrayList. |
private static class | |
private static class |
Modifier and Type | Field and Description |
---|---|
private transient volatile Object[] | array
The array, accessed only via getArray/setArray. |
pack-priv final transient Object | lock
The lock protecting all mutators. |
private static final long |
Access | Constructor and Description |
---|---|
public | |
public | CopyOnWriteArrayList(Collection<? extends E>
the collection of initially held elements c)Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
public | CopyOnWriteArrayList(E[]
the array (a copy of this array is used as the
internal array) toCopyIn)Creates a list holding a copy of the given array. |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
element to be appended to this list e)Implements java. Appends the specified element to the end of this list. |
public void | add(int
index at which the specified element is to be inserted index, E element to be inserted element)Implements java. Inserts the specified element at the specified position in this list. |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be added to this list c)Implements java. 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. |
public boolean | Returns: true if this list changed as a result of the callindex at which to insert the first element
from the specified collection index, Collection<? extends E> collection containing elements to be added to this list c)Implements java. Inserts all of the elements in the specified collection into this list, starting at the specified position. |
public int | Returns: the number of elements addedcollection containing elements to be added to this list 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. |
public void | addFirst(E
the element to be added e)Overrides default java. Adds an element as the first element of this collection (optional operation). |
public boolean | Returns: true if the element was addedelement to be added to this list, if absent e)Appends the element, if not present. |
private boolean | addIfAbsent(E e, Object[] snapshot)
A version of addIfAbsent using the strong hint that given recent snapshot does not contain e. |
public void | addLast(E
the element to be added. e)Overrides default java. Adds an element as the last element of this collection (optional operation). |
private boolean | |
pack-priv boolean | |
public void | |
public Object | Returns: a clone of this listOverrides java. Returns a shallow copy of this list. |
public boolean | Returns: true if this list contains the specified elementelement whose presence in this list is to be tested o)Implements java. Returns |
public boolean | Returns: true if this list contains all of the elements of the
specified collectioncollection to be checked for containment in this list c)Implements java. Returns |
pack-priv static <E> E | |
public boolean | Returns: true if the specified object is equal to this listthe object to be compared for equality with this list o)Overrides java. Implements java. Compares the specified object with this list for equality. |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each element of the |
public E | get(int
index of the element to return index)Implements java. Returns the element at the specified position in this list. |
pack-priv final Object[] | |
public E | |
public E | |
public int | Returns: the hash code value for this listOverrides java. Implements java. Returns the hash code value for this list. |
private static int | |
public int | indexOf(Object
element to search for o)Implements java. Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
public int | Returns: the index of the first occurrence of the element in this list at positionindex or later in the list;
-1 if the element is not found.element to search for e, int index to start searching from index)Returns the index of the first occurrence of the specified element in
this list, searching forwards from |
private static int | Returns: index of element, or -1 if absentelement to search for o, Object[] the array es, int first index to search from, int one past last index to search to)static version of indexOf, to allow repeated calls without needing to re-acquire array each time. |
private static boolean | |
public boolean | Returns: true if this list contains no elementsImplements java. Returns |
public Iterator | Returns: an iterator over the elements in this list in proper sequenceImplements java. Returns an iterator over the elements in this list in proper sequence. |
public int | lastIndexOf(Object
element to search for o)Implements java. Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
public int | Returns: the index of the last occurrence of the element at position less than or equal toindex in this list;
-1 if the element is not found.element to search for e, int index to start searching backwards from index)Returns the index of the last occurrence of the specified element in
this list, searching backwards from |
private static int | Returns: index of element, or -1 if absentelement to search for o, Object[] the array es, int index of first element of range, last element to search from, int one past last element of range, first element to search to)static version of lastIndexOf. |
public ListIterator | listIterator()
Implements java. Returns a list iterator over the elements in this list (in proper sequence). |
public ListIterator | listIterator(int
index of the first element to be returned from the
list iterator (by a call to index)next )Implements java. Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
private static long[] | |
pack-priv static String | |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this list from a stream (that is, deserializes it). |
public E | remove(int
the index of the element to be removed index)Implements java. Removes the element at the specified position in this list. |
public boolean | Returns: true if this list contained the specified elementelement to be removed from this list, if present o)Implements java. Removes the first occurrence of the specified element from this list, if it is present. |
private boolean | |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be removed from this list c)Implements java. Removes from this list all of its elements that are contained in the specified collection. |
public E | removeFirst()
Overrides default java. Removes and returns the first element of this collection (optional operation). |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
public E | removeLast()
Overrides default java. Removes and returns the last element of this collection (optional operation). |
pack-priv void | removeRange(int
index of first element to be removed fromIndex, int index after last element to be removed toIndex)Removes from this list all of the elements whose index is between
|
public void | replaceAll(UnaryOperator<E>
the operator to apply to each element operator)Overrides default java. Replaces each element of this list with the result of applying the operator to that element (optional operation). |
pack-priv void | |
private void | |
public boolean | Returns: true if this list changed as a result of the callcollection containing elements to be retained in this list c)Implements java. Retains only the elements in this list that are contained in the specified collection. |
public List | reversed()
Overrides default java. Returns a reverse-ordered view of this collection. |
public E | set(int
index of the element to replace index, E element to be stored at the specified position element)Implements java. Replaces the element at the specified position in this list with the specified element. |
pack-priv final void | |
private static void | |
public int | Returns: the number of elements in this listImplements java. Returns the number of elements in this list. |
public void | sort(Comparator<? super E>
the c)Comparator used to compare list elements.
A null value indicates that the elements'
natural ordering should be usedOverrides default java. Sorts this list according to the order induced by the specified
|
pack-priv void | |
public Spliterator | Returns: aSpliterator over the elements in this listOverrides default java. Returns a |
public List | Returns: a view of the specified range within this listlow endpoint (inclusive) of the subList fromIndex, int high endpoint (exclusive) of the subList toIndex)Implements java. Returns a view of the portion of this list between
|
public Object[] | Returns: an array containing all the elements in this listImplements java. Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
public <T> T[] | Returns: an array containing all the elements in this listthe array into which the elements of the list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose. a)Implements java. Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. |
public String | Returns: a string representation of this listOverrides java. Returns a string representation of this list. |
private void |