|
||||||||||
| 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
<E>remove

in interface List
<E>o - element to be removed from this list, if present.

public boolean addIfAbsent(E element)
element - element to be added to this Collection, if absent.

public boolean containsAll(Collection![]()
![]()
<?> c)
This implementation iterates over the specified Collection, checking each element returned by the Iterator in turn to see if it's contained in this Collection. If all elements are so contained true is returned, otherwise false.
containsAll

in interface Collection
<E>containsAll

in interface List
<E>c - the collection
Collection.contains(Object)


public boolean removeAll(Collection![]()
![]()
<?> c)
removeAll

in interface Collection
<E>removeAll

in interface List
<E>c - the collection
Collection.remove(Object)
,
Collection.contains(Object)


public boolean retainAll(Collection![]()
![]()
<?> c)
retainAll

in interface Collection
<E>retainAll

in interface List
<E>c - the collection
Collection.remove(Object)
,
Collection.contains(Object)


public int addAllAbsent(Collection![]()
![]()
<? extends E> c)
c - elements to be added into this list.

public void clear()
clear

in interface Collection
<E>clear

in interface List
<E>

public boolean addAll(Collection![]()
![]()
<? extends E> c)
addAll

in interface Collection
<E>addAll

in interface List
<E>c - elements to be inserted into this list.
Collection.add(Object)


public boolean addAll(int index,
Collection
<? extends E> c)
addAll

in interface List
<E>index - index at which to insert first element
from the specified collection.c - elements to be inserted into this list.
IndexOutOfBoundsException

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

public String![]()
![]()
toString()
toString

in class Object


public boolean equals(Object![]()
![]()
o)
This implementation first checks if the specified object is this List. If so, it returns true; if not, it checks if the specified object is a List. If not, it returns false; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns false, this method returns false. If either Iterator runs out of elements before the other it returns false (as the Lists are of unequal length); otherwise it returns true when the iterations complete.
equals

in interface Collection
<E>equals

in interface List
<E>equals

in class Object

o - the Object to be compared for equality with this List.
Object.hashCode()
,
Hashtable


public int hashCode()
This implementation uses the definition in List.hashCode()
.
hashCode

in interface Collection
<E>hashCode

in interface List
<E>hashCode

in class Object

Object.equals(java.lang.Object)
,
Hashtable


public Iterator![]()
![]()
<E> iterator()
iterator

in interface Iterable
<E>iterator

in interface Collection
<E>iterator

in interface List
<E>

public ListIterator![]()
![]()
<E> listIterator()
listIterator

in interface List
<E>

public ListIterator![]()
![]()
<E> listIterator(int index)
listIterator

in interface List
<E>index - index of first element to be returned from the
ListIterator (by a call to getNext).
IndexOutOfBoundsException

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

public List![]()
![]()
<E> subList(int fromIndex, int toIndex)
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 the List, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
subList

in interface List
<E>fromIndex - low endpoint (inclusive) of the subList.toIndex - high endpoint (exclusive) of the subList.
IndexOutOfBoundsException

- Illegal endpoint index value
(fromIndex < 0 || toIndex > size || fromIndex > toIndex).
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||