java.lang.Object java.util.concurrent.CopyOnWriteArrayList
E
- the type of elements held in this collection
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. |
|
Object |
clone()
Returns a shallow copy of this list. |
|
boolean |
contains(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(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(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(Object elem)
Returns the index of the last occurrence of the specified object in this list. |
|
ListIterator<E> |
listIterator()
Returns an Iterator of the elements in this List (in proper sequence). |
|
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(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. |
|
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. |
|
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 List
public boolean isEmpty()
isEmpty
in interface List
public boolean contains(Object elem)
contains
in interface List
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
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
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 List
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 List
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
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
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 List
element
- element to be appended to this list.
public void add(int index, E element)
add
in interface List
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
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 List
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 List
c
- the collection
List.contains(Object)
public boolean removeAll(Collection<?> c)
removeAll
in interface List
c
- the collection
List.remove(Object)
,
List.contains(Object)
public boolean retainAll(Collection<?> c)
retainAll
in interface List
c
- 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 List
public boolean addAll(Collection<? extends E> c)
addAll
in interface List
c
- elements to be inserted into this list.
List.add(Object)
public boolean addAll(int index, Collection<? extends E> c)
addAll
in interface List
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 before the other it returns false (as the Lists are of unequal length); otherwise it returns true when the iterations complete.
equals
in interface List
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 List
hashCode
in class Object
Object.equals(java.lang.Object)
,
Hashtable
public Iterator<E> iterator()
iterator
in interface List
public ListIterator<E> listIterator()
listIterator
in interface List
public ListIterator<E> listIterator(int index)
listIterator
in interface List
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
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).