public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable
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(java.util.Collection<? extends E>) 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, Iterator.remove(), ListIterator.add(E) and ListIterator.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(java.util.Collection<? extends E>)).
| Constructor and Description |
|---|
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. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
Appends the specified element to the end of this list (optional
operation).
|
void |
add(int index,
E e)
Inserts the specified element at the specified position in this list
(optional operation).
|
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).
|
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).
|
int |
addAllAbsent(Collection<? extends E> collection)
Adds the elements of
collection that are not already present in
this list. |
boolean |
addIfAbsent(E object)
Adds
object to the end of this list if it is not already present. |
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.
|
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(E object,
int to)
Searches this list for
object and returns the index of the last
occurrence that is before to. |
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.
|
ListIterator<E> |
listIterator()
Equivalent to
listIterator(0). |
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. |
E |
remove(int index)
Removes the element at the specified position in this list (optional
operation).
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list,
if it is present (optional operation).
|
boolean |
removeAll(Collection<?> collection)
Removes from this list all of its elements that are contained in the
specified collection (optional operation).
|
boolean |
retainAll(Collection<?> collection)
Retains only the elements in this list that are contained in the
specified collection (optional operation).
|
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.
|
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[] |
toArray()
Returns an array containing all of the elements in this list in proper
sequence (from first to last element).
|
<T> T[] |
toArray(T[] contents)
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.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
object.
|
finalize, getClass, notify, notifyAll, wait, wait, waitcopyOf, of, of, of, of, of, of, of, of, of, of, of, of, replaceAll, sort, spliteratorforEach, parallelStream, removeIf, stream, toArraypublic CopyOnWriteArrayList()
public CopyOnWriteArrayList(Collection<? extends E> collection)
collection.public CopyOnWriteArrayList(E[] array)
array.public Object clone()
ObjectObject. The default
implementation returns a so-called "shallow" copy: It creates a new
instance of the same class and then copies the field values (including
object references) from this instance to the new instance. A "deep" copy,
in contrast, would also recursively clone nested objects. A subclass that
needs to implement this kind of cloning should call super.clone()
to create the new instance and then create deep copies of the nested,
mutable objects.public int size()
ListInteger.MAX_VALUE elements, returns
Integer.MAX_VALUE.public E get(int index)
Listpublic boolean contains(Object o)
Listtrue if this list contains the specified element.
More formally, returns true if and only if this list contains
at least one element e such that
Objects.equals(o, e).public boolean containsAll(Collection<?> collection)
Listtrue if this list contains all of the elements of the
specified collection.containsAll in interface Collection<E>containsAll in interface List<E>collection - collection to be checked for containment in this listtrue if this list contains all of the elements of the
specified collectionList.contains(Object)public int indexOf(E object, int from)
object and returns the index of the first
occurrence that is at or after from.public int indexOf(Object object)
Listi such that
Objects.equals(o, get(i)),
or -1 if there is no such index.public int lastIndexOf(E object, int to)
object and returns the index of the last
occurrence that is before to.public int lastIndexOf(Object object)
Listi such that
Objects.equals(o, get(i)),
or -1 if there is no such index.lastIndexOf in interface List<E>object - element to search forpublic boolean isEmpty()
Listtrue if this list contains no elements.public Iterator<E> iterator()
Iterator that iterates over the elements of this list
as they were at the time of this method call. Changes to the list made
after this method call will not be reflected by the iterator, nor will
they trigger a ConcurrentModificationException.
The returned iterator does not support Iterator.remove().
public ListIterator<E> listIterator(int index)
ListIterator that iterates over the elements of this
list as they were at the time of this method call. Changes to the list
made after this method call will not be reflected by the iterator, nor
will they trigger a ConcurrentModificationException.
The returned iterator does not support ListIterator.add(E),
ListIterator.set(E) or Iterator.remove(),
listIterator in interface List<E>index - index of the first element to be returned from the
list iterator (by a call to next)public ListIterator<E> listIterator()
listIterator(0).listIterator in interface List<E>public List<E> subList(int from, int to)
ListfromIndex, inclusive, and toIndex, exclusive. (If
fromIndex and toIndex are equal, the returned list is
empty.) The returned list is backed by this list, so non-structural
changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported
by this list.This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
public Object[] toArray()
ListThe returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray in interface Collection<E>toArray in interface List<E>Arrays.asList(Object[])public <T> T[] toArray(T[] contents)
ListIf the list fits in the specified array with room to spare (i.e.,
the array has more elements than the list), the element in the array
immediately following the end of the list is set to null.
(This is useful in determining the length of the list only if
the caller knows that the list does not contain any null elements.)
Like the List.toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings.
The following code can be used to dump the list into a newly
allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().toArray in interface Collection<E>toArray in interface List<E>T - the runtime type of the array to contain the collectioncontents - the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.public boolean equals(Object other)
Objecto must represent the same object
as this instance using a class-specific comparison. The general contract
is that this comparison should be reflexive, symmetric, and transitive.
Also, no object reference other than null is equal to null.
The default implementation returns true only if this ==
o. See Writing a correct
equals method
if you intend implementing your own equals method.
The general contract for the equals and Object.hashCode() methods is that if equals returns true for
any two objects, then hashCode() must return the same value for
these objects. This means that subclasses of Object usually
override either both methods or neither of them.
equals in interface Collection<E>equals in interface List<E>equals in class Objectother - the object to compare this instance with.true if the specified object is equal to this Object; false otherwise.Object.hashCode()public int hashCode()
ObjectObject.equals(java.lang.Object) returns true must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode method
if you intend implementing your own hashCode method.
hashCode in interface Collection<E>hashCode in interface List<E>hashCode in class ObjectObject.equals(java.lang.Object)public String toString()
ObjectgetClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString method
if you intend implementing your own toString method.
public boolean add(E e)
ListLists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
add in interface Collection<E>add in interface List<E>e - element to be appended to this listtrue (as specified by Collection.add(E))public void add(int index,
E e)
Listpublic boolean addAll(Collection<? extends E> collection)
ListaddAll in interface Collection<E>addAll in interface List<E>collection - collection containing elements to be added to this listtrue if this list changed as a result of the callList.add(Object)public boolean addAll(int index,
Collection<? extends E> collection)
Listpublic int addAllAbsent(Collection<? extends E> collection)
collection that are not already present in
this list. If collection includes a repeated value, at most one
occurrence of that value will be added to this list. Elements are added
at the end of this list.
Callers of this method may prefer CopyOnWriteArraySet, whose
API is more appropriate for set operations.
public boolean addIfAbsent(E object)
object to the end of this list if it is not already present.
Callers of this method may prefer CopyOnWriteArraySet, whose
API is more appropriate for set operations.
public void clear()
Listpublic E remove(int index)
Listpublic boolean remove(Object o)
Listi such that
Objects.equals(o, get(i))
(if such an element exists). Returns true if this list
contained the specified element (or equivalently, if this list changed
as a result of the call).public boolean removeAll(Collection<?> collection)
ListremoveAll in interface Collection<E>removeAll in interface List<E>collection - collection containing elements to be removed from this listtrue if this list changed as a result of the callList.remove(Object),
List.contains(Object)public boolean retainAll(Collection<?> collection)
ListretainAll in interface Collection<E>retainAll in interface List<E>collection - collection containing elements to be retained in this listtrue if this list changed as a result of the callList.remove(Object),
List.contains(Object)