Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class CopyOnWriteArrayList<E>

extends Object
implements List<E>, RandomAccess, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, java.util.RandomAccess, java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type of elements held in this list
Imports
java.lang.invoke.VarHandle, java.lang.reflect.Field, java.util.ArrayList, .Arrays, .Collection, .Collections, .Comparator, .ConcurrentModificationException, .Iterator, .List, .ListIterator, .NoSuchElementException, .Objects, .RandomAccess, .Spliterator, .Spliterators, java.util.function.Consumer, .IntFunction, .Predicate, .UnaryOperator, java.util.stream.Stream, .StreamSupport, jdk.internal.access.SharedSecrets, jdk.internal.util.ArraysSupport

A thread-safe variant of 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.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
private class
CopyOnWriteArrayList.COWSubList

Sublist for CopyOnWriteArrayList.

private static class
private static class
CopyOnWriteArrayList.Reversed<E>

Reversed view for CopyOnWriteArrayList and its sublists.

Field Summary

Modifier and TypeField 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

Constructor Summary

AccessConstructor and Description
public
CopyOnWriteArrayList()

Creates an empty list.

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.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true (as specified by Collection#add)
add
(E
element to be appended to this list
e
)

Implements java.util.List.add.

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.util.List.add.

Inserts the specified element at the specified position in this list.

public boolean

Returns:

true if this list changed as a result of the call
addAll
(Collection<? extends E>
collection containing elements to be added to this list
c
)

Implements java.util.List.addAll.

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 call
addAll
(int
index 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.util.List.addAll.

Inserts all of the elements in the specified collection into this list, starting at the specified position.

public int

Returns:

the number of elements added
addAllAbsent
(Collection<? extends E>
collection 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.util.List.addFirst.

Adds an element as the first element of this collection (optional operation).

public boolean

Returns:

true if the element was added
addIfAbsent
(E
element 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.util.List.addLast.

Adds an element as the last element of this collection (optional operation).

private boolean
bulkRemove(Predicate<? super E> filter)

pack-priv boolean
bulkRemove(Predicate<? super E> filter, int i, int end)

public void
clear()

Implements java.util.List.clear.

Removes all of the elements from this list.

public Object

Returns:

a clone of this list
clone
()

Overrides java.lang.Object.clone.

Returns a shallow copy of this list.

public boolean

Returns:

true if this list contains the specified element
contains
(Object
element whose presence in this list is to be tested
o
)

Implements java.util.List.contains.

Returns true if this list contains the specified element.

public boolean

Returns:

true if this list contains all of the elements of the specified collection
containsAll
(Collection<?>
collection to be checked for containment in this list
c
)

Implements java.util.List.containsAll.

Returns true if this list contains all of the elements of the specified collection.

pack-priv static <E> E
elementAt(Object[] a, int index)

public boolean

Returns:

true if the specified object is equal to this list
equals
(Object
the object to be compared for equality with this list
o
)

Overrides java.lang.Object.equals.

Implements java.util.List.equals.

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.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

public E
get(int
index of the element to return
index
)

Implements java.util.List.get.

Returns the element at the specified position in this list.

pack-priv final Object[]
getArray()

Gets the array.

public E
getFirst()

Overrides default java.util.List.getFirst.

Gets the first element of this collection.

public E
getLast()

Overrides default java.util.List.getLast.

Gets the last element of this collection.

public int

Returns:

the hash code value for this list
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode.

Returns the hash code value for this list.

private static int
hashCodeOfRange(Object[] es, int from, int to)

public int
indexOf(Object
element to search for
o
)

Implements java.util.List.indexOf.

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 position index or later in the list; -1 if the element is not found.
indexOf
(E
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 index, or returns -1 if the element is not found.

private static int

Returns:

index of element, or -1 if absent
indexOfRange
(Object
element 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
isClear(long[] bits, int i)

public boolean

Returns:

true if this list contains no elements
isEmpty
()

Implements java.util.List.isEmpty.

Returns true if this list contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this list in proper sequence
iterator
()

Implements java.util.List.iterator.

Returns an iterator over the elements in this list in proper sequence.

public int
lastIndexOf(Object
element to search for
o
)

Implements java.util.List.lastIndexOf.

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 to index in this list; -1 if the element is not found.
lastIndexOf
(E
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 index, or returns -1 if the element is not found.

private static int

Returns:

index of element, or -1 if absent
lastIndexOfRange
(Object
element 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<E>
listIterator()

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).

public ListIterator<E>
listIterator(int
index of the first element to be returned from the list iterator (by a call to next)
index
)

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

private static long[]
nBits(int n)

pack-priv static String
outOfBounds(int index, int size)

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.util.List.remove.

Removes the element at the specified position in this list.

public boolean

Returns:

true if this list contained the specified element
remove
(Object
element to be removed from this list, if present
o
)

Implements java.util.List.remove.

Removes the first occurrence of the specified element from this list, if it is present.

private boolean
remove(Object o, Object[] snapshot, int index)

A version of remove(Object) using the strong hint that given recent snapshot contains o at the given index.

public boolean

Returns:

true if this list changed as a result of the call
removeAll
(Collection<?>
collection containing elements to be removed from this list
c
)

Implements java.util.List.removeAll.

Removes from this list all of its elements that are contained in the specified collection.

public E
removeFirst()

Overrides default java.util.List.removeFirst.

Removes and returns the first element of this collection (optional operation).

public boolean
removeIf(Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation).

public E
removeLast()

Overrides default java.util.List.removeLast.

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 fromIndex, inclusive, and toIndex, exclusive.

public void
replaceAll(UnaryOperator<E>
the operator to apply to each element
operator
)

Overrides default java.util.List.replaceAll.

Replaces each element of this list with the result of applying the operator to that element (optional operation).

pack-priv void
replaceAllRange(UnaryOperator<E> operator, int i, int end)

private void
resetLock()

Initializes the lock; for use when deserializing or cloning.

public boolean

Returns:

true if this list changed as a result of the call
retainAll
(Collection<?>
collection containing elements to be retained in this list
c
)

Implements java.util.List.retainAll.

Retains only the elements in this list that are contained in the specified collection.

public List<E>
reversed()

Overrides default java.util.List.reversed.

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.util.List.set.

Replaces the element at the specified position in this list with the specified element.

pack-priv final void
setArray(Object[] a)

Sets the array.

private static void
setBit(long[] bits, int i)

public int

Returns:

the number of elements in this list
size
()

Implements java.util.List.size.

Returns the number of elements in this list.

public void
sort(Comparator<? super E>
the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used
c
)

Overrides default java.util.List.sort.

Sorts this list according to the order induced by the specified Comparator (optional operation).

pack-priv void
sortRange(Comparator<? super E> c, int i, int end)

public Spliterator<E>

Returns:

a Spliterator over the elements in this list
spliterator
()

Overrides default java.util.List.spliterator.

Returns a Spliterator over the elements in this list.

public List<E>

Returns:

a view of the specified range within this list
subList
(int
low endpoint (inclusive) of the subList
fromIndex
,
int
high endpoint (exclusive) of the subList
toIndex
)

Implements java.util.List.subList.

Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.

public Object[]

Returns:

an array containing all the elements in this list
toArray
()

Implements java.util.List.toArray.

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 list
toArray
(T[]
the 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.util.List.toArray.

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 list
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this list.

private void
writeObject(ObjectOutputStream
the stream
s
)

Saves this list to a stream (that is, serializes it).

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait