java.util.concurrent
public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable
remove
, add
etc..) operation
is performed.O(n)
or worse,
but traversal operations are fast and efficient, especially when running in
a multi-thread environment without the need to design complex synchronize
mechanisms.Iterator
s in this class work on a snapshot of the backing store
at the moment the iterator itself was created, hence the iterator will not
reflect changes in the underlying storage. Thus, update operation on the
Iterator
s are not supported, but as interferences from other
threads are impossible, no ConcurrentModificationException
will be ever thrown from within the Iterator
.
CopyOnWriteArrayList listeners =
new CopyOnWriteArrayList();
[...]
for (final EventListener listener : listeners)
{
Runnable dispatcher = new Runnable() {
public void run()
{
listener.preferenceChange(event);
}
};
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(dispatcher);
}
Since: 1.5
Constructor Summary | |
---|---|
CopyOnWriteArrayList()
Construct a new ArrayList with the default capacity (16). | |
CopyOnWriteArrayList(Collection<? extends E> c)
Construct a new ArrayList, and initialize it with the elements in the
supplied Collection. | |
CopyOnWriteArrayList(E[] array)
Construct a new ArrayList, and initialize it with the elements in the
supplied array.
|
Method Summary | |
---|---|
boolean | add(E e)
Appends the supplied element to the end of this list. |
void | add(int index, E e)
Adds the supplied element at the specified index, shifting all elements
currently at that index or higher one to the right. |
boolean | addAll(Collection<? extends E> c)
Add each element in the supplied Collection to this List. |
boolean | addAll(int index, Collection<? extends E> c)
Add all elements in the supplied collection, inserting them beginning at
the specified index. c can contain objects of any type, as well as null
values.
|
int | addAllAbsent(Collection<? extends E> c)
Adds all the element from the given collection that are not already
in this list.
|
boolean | addIfAbsent(E val)
Adds an element if the list does not contains it already.
|
void | clear()
Removes all elements from this List |
Object | clone()
Creates a shallow copy of this ArrayList (elements are not cloned).
|
boolean | contains(Object e)
Returns true if element is in this ArrayList.
|
boolean | containsAll(Collection<?> c)
Tests whether this collection contains all the elements in a given
collection. |
boolean | equals(Object o) |
E | get(int index)
Retrieves the element at the user-supplied index.
|
int | hashCode() |
int | indexOf(Object e)
Returns the lowest index at which element appears in this List, or -1 if it
does not appear.
|
int | indexOf(E e, int index)
Return the lowest index greater equal index at which
e appears in this List, or -1 if it does not
appear.
|
boolean | isEmpty()
Checks if the list is empty.
|
Iterator<E> | iterator()
Return an Iterator containing the elements of this list.
|
int | lastIndexOf(Object e)
Returns the highest index at which element appears in this List, or -1 if
it does not appear.
|
int | lastIndexOf(E e, int index)
Returns the highest index lesser equal index at
which e appears in this List, or -1 if it does not
appear.
|
ListIterator<E> | listIterator()
Return a ListIterator containing the elements of this list.
|
ListIterator<E> | listIterator(int index)
Return a ListIterator over the elements of this list starting at
the specified index. |
E | remove(int index)
Removes the element at the user-supplied index.
|
boolean | remove(Object element)
Remove the first occurrence, if any, of the given object from this list,
returning true if the object was removed, false
otherwise.
|
boolean | removeAll(Collection<?> c)
Removes all the elements contained in the given collection.
|
boolean | retainAll(Collection<?> c)
Removes all the elements that are not in the passed collection.
|
E | set(int index, E e)
Sets the element at the specified index. |
int | size()
Returns the number of elements in this list.
|
List<E> | subList(int fromIndex, int toIndex)
Obtain a List view of a subsection of this list, from fromIndex
(inclusive) to toIndex (exclusive). |
Object[] | toArray()
Returns an Object array containing all of the elements in this ArrayList.
|
<T> T[] | toArray(T[] a)
Returns an Array whose component type is the runtime component type of the
passed-in Array. |
String | toString() |
Parameters: c the collection whose elements will initialize this list
Throws: NullPointerException if c is null
Parameters: array the array used to initialize this list
Throws: NullPointerException if array is null
Parameters: e the element to be appended to this list
Returns: true, the add will always succeed
Parameters: index the index at which the element is being added e the item being added
Throws: IndexOutOfBoundsException if index < 0 || index > size()
Parameters: c a Collection containing elements to be added to this List
Returns: true if the list was modified, in other words c is not empty
Throws: NullPointerException if c is null
Parameters: index the index at which the elements will be inserted c the Collection containing the elements to be inserted
Throws: IndexOutOfBoundsException if index < 0 || index > 0 NullPointerException if c is null
Parameters: c the Collection containing the elements to be inserted
Returns: true the list internal storage changed as a result of this operation, false otherwise.
Parameters: val the element to add to the list.
Returns: true if the element was added, false otherwise.
Returns: the cloned object
Parameters: e the element whose inclusion in the List is being tested
Returns: true if the list contains e
Parameters: c the collection to test against
Returns: true if this collection contains all the elements in the given collection
Throws: NullPointerException if the given collection is null
See Also: contains
Parameters: index the index of the element we are fetching
Throws: IndexOutOfBoundsException if index < 0 || index >= size()
Parameters: e the element whose inclusion in the List is being tested
Returns: the index where e was found
index
at which
e
appears in this List, or -1 if it does not
appear.
Parameters: e the element whose inclusion in the list is being tested index the index at which the search begins
Returns: the index where e
was found
Returns: true if there are no elements
Returns: an Iterator containing the elements of this list in sequence.
Parameters: e the element whose inclusion in the List is being tested
Returns: the index where e was found
index
at
which e
appears in this List, or -1 if it does not
appear.
Parameters: e the element whose inclusion in the list is being tested index the index at which the search begins
Returns: the index where e
was found
Returns: a ListIterator containing the elements of this list in sequence.
Parameters: index the index at which to start iterating.
Returns: a ListIterator containing the elements of this list in sequence.
Parameters: index the index of the element to be removed
Returns: the removed Object
Throws: IndexOutOfBoundsException if index < 0 || index >= size()
true
if the object was removed, false
otherwise.
Parameters: element the object to be removed.
Returns: true if element was removed, false otherwise. false means also that the underlying storage was unchanged after this operation concluded.
Parameters: c the collection containing the elements to be removed from this list.
Returns: true if at least one element was removed, indicating that the list internal storage changed as a result, false otherwise.
clear()
.
Please, note that this method is extremely slow (unless the argument has
size == 0
) and has bad performance is both space and time
usage.
Parameters: c the collection containing the elements to be retained by this list.
Returns: true the list internal storage changed as a result of this operation, false otherwise.
Parameters: index the index at which the element is being set e the element to be set
Returns: the element previously at the specified index
Throws: IndexOutOfBoundsException if index < 0 || index >= 0
Returns: the list size
This implementation returns a subclass of AbstractList. It stores, in private fields, the offset and size of the sublist, and the expected modCount of the backing list. If the backing list implements RandomAccess, the sublist will also.
The subclass's set(int, Object)
, get(int)
,
add(int, Object)
, remove(int)
,
addAll(int, Collection)
and
removeRange(int, int)
methods all delegate to the
corresponding methods on the backing abstract list, after
bounds-checking the index and adjusting for the offset. The
addAll(Collection c)
method merely returns addAll(size, c).
The listIterator(int)
method returns a "wrapper object"
over a list iterator on the backing list, which is created with the
corresponding method on the backing list. The iterator()
method merely returns listIterator(), and the size()
method
merely returns the subclass's size field.
All methods first check to see if the actual modCount of the backing list is equal to its expected value, and throw a ConcurrentModificationException if it is not.
Parameters: fromIndex the index that the returned list should start from (inclusive) toIndex the index that the returned list should go to (exclusive)
Returns: a List backed by a subsection of this list
Throws: IndexOutOfBoundsException if fromIndex < 0 || toIndex > size() IndexOutOfBoundsException if fromIndex > toIndex
See Also: ConcurrentModificationException RandomAccess
Returns: an array representation of this list
Parameters: a the passed-in Array
Returns: an array representation of this list
Throws: ArrayStoreException if the runtime type of a does not allow an element in this list NullPointerException if a is null