|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.concurrent.CopyOnWriteArrayList
A 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.
| Constructor Summary | |
CopyOnWriteArrayList()
Creates an empty list. |
|
CopyOnWriteArrayList(Collection<E> c)
Creates a list containing the elements of the specified Collection, in the order they are returned by the Collection's iterator. |
|
CopyOnWriteArrayList(E[] toCopyIn)
Create a new CopyOnWriteArrayList holding a copy of given array. |
|
| Method Summary | ||
boolean |
add(E element)
Appends the specified element to the end of this list. |
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this list. |
|
boolean |
addAll(Collection<? extends E> c)
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(int index,
Collection<? extends E> c)
Inserts all of the elements in the specified Collection into this list, starting at the specified position. |
|
int |
addAllAbsent(Collection<? extends E> 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. |
|
boolean |
addIfAbsent(E element)
Append the element if not present. |
|
void |
clear()
Removes all of the elements from this list. |
|
java.lang.Object |
clone()
Returns a shallow copy of this list. |
|
boolean |
contains(java.lang.Object elem)
Returns true if this list contains the specified element. |
|
boolean |
containsAll(Collection<?> c)
Returns true if this Collection contains all of the elements in the specified Collection. |
|
boolean |
equals(java.lang.Object o)
Compares the specified Object with this List for equality. |
|
E |
get(int index)
Returns the element at the specified position in this list. |
|
int |
hashCode()
Returns the hash code value for this List. |
|
int |
indexOf(E elem,
int index)
Searches for the first occurrence of the given argument, beginning the search at index, and testing for equality using the equals method. |
|
int |
indexOf(java.lang.Object elem)
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<E> |
iterator()
Returns an Iterator over the elements contained in this collection. |
|
int |
lastIndexOf(E elem,
int index)
Searches backwards for the specified object, starting from the specified index, and returns an index to it. |
|
int |
lastIndexOf(java.lang.Object elem)
Returns the index of the last occurrence of the specified object in this list. |
|
java.util.ListIterator<E> |
listIterator()
Returns an Iterator of the elements in this List (in proper sequence). |
|
java.util.ListIterator<E> |
listIterator(int index)
Returns a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the List. |
|
E |
remove(int index)
Removes the element at the specified position in this list. |
|
boolean |
remove(java.lang.Object o)
Removes a single instance of the specified element from this list, if it is present (optional operation). |
|
boolean |
removeAll(Collection<?> c)
Removes from this Collection all of its elements that are contained in the specified Collection. |
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this Collection that are contained in the specified Collection (optional operation). |
|
E |
set(int index,
E element)
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<E> |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this list in the correct order. |
|
|
toArray(T[] a)
Returns an array containing all of the elements in this list in the correct order. |
|
java.lang.String |
toString()
Returns a string representation of this Collection, containing the String representation of each element. |
|
| Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public CopyOnWriteArrayList()
public CopyOnWriteArrayList(Collection<E> c)
c - the collection of initially held elementspublic 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 Listpublic boolean isEmpty()
isEmpty in interface Listpublic boolean contains(java.lang.Object elem)
contains in interface Listelem - element whose presence in this List is to be tested.
true if the specified element is present;
false otherwise.public int indexOf(java.lang.Object elem)
indexOf in interface Listelem - 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(java.lang.Object elem)
lastIndexOf in interface Listelem - the desired element.
public int lastIndexOf(E elem,
int index)
elem - the desired element.index - the index to start searching from.
public java.lang.Object clone()
clone in class java.lang.Objectpublic java.lang.Object[] toArray()
toArray in interface ListArrays#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 Lista - 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.
java.lang.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 Listindex - index of element to return.
java.lang.IndexOutOfBoundsException - if index is out of range (index
< 0 || index >= size()).
public E set(int index,
E element)
set in interface Listindex - index of element to replace.element - element to be stored at the specified position.
java.lang.IndexOutOfBoundsException - if index out of range
(index < 0 || index >= size()).public boolean add(E element)
add in interface Listelement - element to be appended to this list.
public void add(int index,
E element)
add in interface Listindex - index at which the specified element is to be inserted.element - element to be inserted.
java.lang.IndexOutOfBoundsException - if index is out of range
(index < 0 || index > size()).public E remove(int index)
remove in interface Listindex - the index of the element to removed.
java.lang.IndexOutOfBoundsException - if index out of range (index
< 0 || index >= size()).public boolean remove(java.lang.Object o)
remove in interface Listo - 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 Listc - the collection
List.contains(Object)public boolean removeAll(Collection<?> c)
removeAll in interface Listc - the collection
List.remove(Object),
List.contains(Object)public boolean retainAll(Collection<?> c)
retainAll in interface Listc - the collection
List.remove(Object),
List.contains(Object)public int addAllAbsent(Collection<? extends E> c)
c - elements to be added into this list.
public void clear()
clear in interface Listpublic boolean addAll(Collection<? extends E> c)
addAll in interface Listc - elements to be inserted into this list.
List.add(Object)
public boolean addAll(int index,
Collection<? extends E> c)
addAll in interface Listindex - index at which to insert first element
from the specified collection.c - elements to be inserted into this list.
java.lang.IndexOutOfBoundsException - index out of range (index
< 0 || index > size()).public java.lang.String toString()
toString in class java.lang.Objectpublic boolean equals(java.lang.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 before the other it returns false (as the Lists are of unequal length); otherwise it returns true when the iterations complete.
equals in interface Listequals in class java.lang.Objecto - the Object to be compared for equality with this List.
public int hashCode()
This
implementation uses the definition in List.hashCode().
hashCode in interface ListhashCode in class java.lang.Objectpublic Iterator<E> iterator()
iterator in interface Listpublic java.util.ListIterator<E> listIterator()
listIterator in interface Listpublic java.util.ListIterator<E> listIterator(int index)
listIterator in interface Listindex - index of first element to be returned from the
ListIterator (by a call to getNext).
java.lang.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 ListfromIndex - low endpoint (inclusive) of the subList.toIndex - high endpoint (exclusive) of the subList.
java.lang.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 | |||||||||