CopyOnWriteArrayList - JRE Emulation | JRE Emulation
public class

CopyOnWriteArrayList

extends Object
implements List<E> RandomAccess Cloneable Serializable
java.lang.Object
   ↳ java.util.concurrent.CopyOnWriteArrayList<E>

Class Overview

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)).

Summary

Public Constructors
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 Methods
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.
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 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.
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.
Inherited Methods
[Expand]
From class java.lang.Object
From interface java.util.List
From interface java.util.Collection
From interface java.lang.Iterable

Public Constructors

public CopyOnWriteArrayList ()

Creates a new empty instance.

public CopyOnWriteArrayList (Collection<? extends E> collection)

Creates a new instance containing the elements of collection.

public CopyOnWriteArrayList (E[] array)

Creates a new instance containing the elements of array.

Public Methods

public synchronized boolean add (E e)

Appends the specified element to the end of this list (optional operation).

Lists 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.

Parameters
e element to be appended to this list
Returns

public synchronized void add (int index, E e)

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters
index index at which the specified element is to be inserted
e element to be inserted

public 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). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters
collection collection containing elements to be added to this list
Returns
  • true if this list changed as a result of the call

public 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). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters
index index at which to insert the first element from the specified collection
collection collection containing elements to be added to this list
Returns
  • true if this list changed as a result of the call

public synchronized int addAllAbsent (Collection<? extends E> collection)

Adds the elements of 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 synchronized boolean addIfAbsent (E object)

Adds 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 synchronized void clear ()

Removes all of the elements from this list (optional operation). The list will be empty after this call returns.

public Object clone ()

Creates and returns a copy of this Object. 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.

Returns
  • a copy of this object.

public boolean contains (Object o)

Returns true 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 (o==null ? e==null : o.equals(e)).

Parameters
o element whose presence in this list is to be tested
Returns
  • true if this list contains the specified element

public boolean containsAll (Collection<?> collection)

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

Parameters
collection collection to be checked for containment in this list
Returns
  • true if this list contains all of the elements of the specified collection

public boolean equals (Object other)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o 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 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.

Parameters
other the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public E get (int index)

Returns the element at the specified position in this list.

Parameters
index index of the element to return
Returns
  • the element at the specified position in this list

public int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(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.

Returns
  • this object's hash code.

public 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.

Returns
  • the index or -1 if the object was not found.

public 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. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters
object element to search for
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 boolean isEmpty ()

Returns true if this list contains no elements.

Returns
  • true if this list contains no elements

public Iterator<E> iterator ()

Returns an 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 remove().

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

public 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. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters
object element to search for
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 lastIndexOf (E object, int to)

Searches this list for object and returns the index of the last occurrence that is before to.

Returns
  • the index or -1 if the object was not found.

public 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. 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 add(E), set(E) or remove(),

Parameters
index index of the first element to be returned from the list iterator (by a call to next)
Returns
  • a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

public ListIterator<E> listIterator ()

Equivalent to listIterator(0).

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

public synchronized E remove (int index)

Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters
index the index of the element to be removed
Returns
  • the element previously at the specified position

public synchronized boolean remove (Object o)

Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(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).

Parameters
o element to be removed from this list, if present
Returns
  • true if this list contained the specified element

public synchronized boolean removeAll (Collection<?> collection)

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

Parameters
collection collection containing elements to be removed from this list
Returns
  • true if this list changed as a result of the call

public synchronized boolean retainAll (Collection<?> collection)

Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.

Parameters
collection collection containing elements to be retained in this list
Returns
  • true if this list changed as a result of the call

public synchronized E set (int index, E e)

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters
index index of the element to replace
e element to be stored at the specified position
Returns
  • the element previously at the specified position

public int size ()

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns
  • the number of elements in this list

public List<E> subList (int from, int to)

Returns a view of the portion of this list between the specified fromIndex, 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.)

Parameters
from low endpoint (inclusive) of the subList
to high endpoint (exclusive) of the subList
Returns
  • a view of the specified range within this list

public Object[] toArray ()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

The 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.

Returns
  • an array containing all of the elements in this list in proper sequence

public 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. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If 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 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().

Parameters
contents 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.
Returns
  • an array containing the elements of this list

public String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.