|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.util.concurrent.CopyOnWriteArrayList<E>
E - the type of elements held in this collection
, Cloneable
, Iterable
<E>, Collection
<E>, List
<E>, RandomAccess

public class CopyOnWriteArrayList<E>

<E>, RandomAccess
, Cloneable
, Serializable

A thread-safe variant of 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.
This class is a member of the Java Collections Framework.
| Constructor Summary | |
|---|---|
CopyOnWriteArrayList
Creates an empty list. |
|
CopyOnWriteArrayList
Creates a list containing the elements of the specified Collection, in the order they are returned by the Collection's iterator. |
|
CopyOnWriteArrayList
Create a new CopyOnWriteArrayList holding a copy of given array. |
|
| Method Summary | ||
|---|---|---|
boolean |
add
Appends the specified element to the end of this list. |
|
void |
add
Inserts the specified element at the specified position in this list. |
|
boolean |
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. |
|
boolean |
addAll
Inserts all of the elements in the specified Collection into this list, starting at the specified position. |
|
int |
addAllAbsent
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. |
|
boolean |
addIfAbsent
Append the element if not present. |
|
void |
clear
Removes all of the elements from this list. |
|
Object |
clone
Returns a shallow copy of this list. |
|
boolean |
contains
Returns true if this list contains the specified element. |
|
boolean |
containsAll
Returns true if this Collection contains all of the elements in the specified Collection. |
|
boolean |
equals
Compares the specified Object with this List for equality. |
|
E |
get
Returns the element at the specified position in this list. |
|
int |
hashCode
Returns the hash code value for this List. |
|
int |
indexOf
Searches for the first occurrence of the given argument, beginning the search at index, and testing for equality using the equals method. |
|
int |
indexOf
Searches for the first occurrence of the given argument, testing for equality using the equals method. |
|
boolean |
isEmpty
Tests if this list has no elements. |
|
Iterator |
iterator
Returns an Iterator over the elements contained in this collection. |
|
int |
lastIndexOf
Searches backwards for the specified object, starting from the specified index, and returns an index to it. |
|
int |
lastIndexOf
Returns the index of the last occurrence of the specified object in this list. |
|
ListIterator |
listIterator
Returns an Iterator of the elements in this List (in proper sequence). |
|
ListIterator |
listIterator
Returns a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the List. |
|
E |
remove
Removes the element at the specified position in this list. |
|
boolean |
remove
Removes a single instance of the specified element from this list, if it is present (optional operation). |
|
boolean |
removeAll
Removes from this Collection all of its elements that are contained in the specified Collection. |
|
boolean |
retainAll
Retains only the elements in this Collection that are contained in the specified Collection (optional operation). |
|
E |
set
Replaces the element at the specified position in this list with the specified element. |
|
int |
size
Returns the number of elements in this list. |
|
List |
subList
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
|
Object |
toArray
Returns an array containing all of the elements in this list in the correct order. |
|
|
toArray
Returns an array containing all of the elements in this list in the correct order. |
|
String |
toString
Returns a string representation of this Collection, containing the String representation of each element. |
|
Methods inherited from class java.lang.Object ![]() |
|---|
finalize |
| Constructor Detail |
|---|

public CopyOnWriteArrayList()

public CopyOnWriteArrayList(Collection![]()
![]()
<? extends E> c)
c - the collection of initially held elements

public CopyOnWriteArrayList(E[] toCopyIn)
toCopyIn - the array (a copy of this array is used as the
internal array)| Method Detail |
|---|

public int size()
size

in interface Collection
<E>size

in interface List
<E>

public boolean isEmpty()
isEmpty

in interface Collection
<E>isEmpty

in interface List
<E>

public boolean contains(Object![]()
![]()
elem)
contains

in interface Collection
<E>contains

in interface List
<E>elem - element whose presence in this List is to be tested.
true if the specified element is present;
false otherwise.

public int indexOf(Object![]()
![]()
elem)
indexOf

in interface List
<E>elem - an object.
Object.equals(Object)


public int indexOf(E elem,
int index)
elem - an object.index - the index to start searching from.
Object.equals(Object)


public int lastIndexOf(Object![]()
![]()
elem)
lastIndexOf

in interface List
<E>elem - the desired element.

public int lastIndexOf(E elem,
int index)
elem - the desired element.index - the index to start searching from.

public Object![]()
![]()
clone()
clone

in class Object

Cloneable


public Object![]()
![]()
[] toArray()
toArray

in interface Collection
<E>toArray

in interface List
<E>Arrays.asList(Object[])


public <T> T[] toArray(T[] a)
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 collection 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.
toArray

in interface Collection
<E>toArray

in interface List
<E>a - 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.
ArrayStoreException

- the runtime type of a is not a supertype
of the runtime type of every element in this list.

public E get(int index)
get

in interface List
<E>index - index of element to return.
IndexOutOfBoundsException

- if index is out of range (index
< 0 || index >= size()).

public E set(int index,
E element)
set

in interface List
<E>index - index of element to replace.element - element to be stored at the specified position.
IndexOutOfBoundsException

- if index out of range
(index < 0 || index >= size()).

public boolean add(E element)
add

in interface Collection
<E>add

in interface List
<E>element - element to be appended to this list.

public void add(int index,
E element)
add

in interface List
<E>index - index at which the specified element is to be inserted.element - element to be inserted.
IndexOutOfBoundsException

- if index is out of range
(index < 0 || index > size()).

public E remove(int index)
remove

in interface List
<E>index - the index of the element to removed.
IndexOutOfBoundsException

- if index out of range (index
< 0 || index >= size()).

public boolean remove(Object![]()
![]()
o)
remove

in interface Collection
![]()