public class CopyOnWriteArrayList<E> extends java.lang.Object implements java.util.List<E>, java.util.RandomAccess, java.lang.Cloneable, java.io.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.Iterators 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
Iterators 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);
}
| Constructor and Description |
|---|
CopyOnWriteArrayList()
Construct a new ArrayList with the default capacity (16).
|
CopyOnWriteArrayList(java.util.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.
|
| Modifier and Type | Method and Description |
|---|---|
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(java.util.Collection<? extends E> c)
Add each element in the supplied Collection to this List.
|
boolean |
addAll(int index,
java.util.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(java.util.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
|
java.lang.Object |
clone()
Creates a shallow copy of this ArrayList (elements are not cloned).
|
boolean |
contains(java.lang.Object e)
Returns true if element is in this ArrayList.
|
boolean |
containsAll(java.util.Collection<?> c)
Tests whether this collection contains all the elements in a given
collection.
|
boolean |
equals(java.lang.Object o)
Determine whether this Object is semantically equal
to another Object.
|
E |
get(int index)
Retrieves the element at the user-supplied index.
|
int |
hashCode()
Get a value that represents this Object, as uniquely as
possible within the confines of an int.
|
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. |
int |
indexOf(java.lang.Object e)
Returns the lowest index at which element appears in this List, or -1 if it
does not appear.
|
boolean |
isEmpty()
Checks if the list is empty.
|
java.util.Iterator<E> |
iterator()
Return an Iterator containing the elements of this list.
|
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. |
int |
lastIndexOf(java.lang.Object e)
Returns the highest index at which element appears in this List, or -1 if
it does not appear.
|
java.util.ListIterator<E> |
listIterator()
Return a ListIterator containing the elements of this list.
|
java.util.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(java.lang.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(java.util.Collection<?> c)
Removes all the elements contained in the given collection.
|
boolean |
retainAll(java.util.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.
|
java.util.List<E> |
subList(int fromIndex,
int toIndex)
Obtain a List view of a subsection of this list, from fromIndex
(inclusive) to toIndex (exclusive).
|
java.lang.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.
|
java.lang.String |
toString()
Convert this Object to a human-readable String.
|
public CopyOnWriteArrayList()
public CopyOnWriteArrayList(java.util.Collection<? extends E> c)
c - the collection whose elements will initialize this listjava.lang.NullPointerException - if c is nullpublic CopyOnWriteArrayList(E[] array)
array - the array used to initialize this listjava.lang.NullPointerException - if array is nullpublic int size()
public boolean isEmpty()
public boolean contains(java.lang.Object e)
public boolean containsAll(java.util.Collection<?> c)
containsAll in interface java.util.Collection<E>containsAll in interface java.util.List<E>c - the collection to test againstjava.lang.NullPointerException - if the given collection is nullcontains(Object)public int indexOf(java.lang.Object e)
indexOf in interface java.util.List<E>e - the element whose inclusion in the List is being testedpublic int indexOf(E e, int index)
index at which
e appears in this List, or -1 if it does not
appear.e - the element whose inclusion in the list is being testedindex - the index at which the search beginse was foundpublic int lastIndexOf(java.lang.Object e)
lastIndexOf in interface java.util.List<E>e - the element whose inclusion in the List is being testedpublic int lastIndexOf(E e, int index)
index at
which e appears in this List, or -1 if it does not
appear.e - the element whose inclusion in the list is being testedindex - the index at which the search beginse was foundpublic java.lang.Object clone()
clone in class java.lang.ObjectCloneablepublic java.lang.Object[] toArray()
public <T> T[] toArray(T[] a)
toArray in interface java.util.Collection<E>toArray in interface java.util.List<E>a - the passed-in Arrayjava.lang.ArrayStoreException - if the runtime type of a does not allow an element in this listjava.lang.NullPointerException - if a is nullpublic E get(int index)
get in interface java.util.List<E>index - the index of the element we are fetchingjava.lang.IndexOutOfBoundsException - if index < 0 || index >= size()public E set(int index, E e)
set in interface java.util.List<E>index - the index at which the element is being sete - the element to be setjava.lang.IndexOutOfBoundsException - if index < 0 || index >= 0public boolean add(E e)
public void add(int index, E e)
add in interface java.util.List<E>index - the index at which the element is being addede - the item being addedjava.lang.IndexOutOfBoundsException - if index < 0 || index > size()public E remove(int index)
remove in interface java.util.List<E>index - the index of the element to be removedjava.lang.IndexOutOfBoundsException - if index < 0 || index >= size()public boolean remove(java.lang.Object element)
true if the object was removed, false
otherwise.remove in interface java.util.Collection<E>remove in interface java.util.List<E>element - the object to be removed.public boolean removeAll(java.util.Collection<?> c)
removeAll in interface java.util.Collection<E>removeAll in interface java.util.List<E>c - the collection containing the elements to be removed from this
list.List.remove(Object),
List.contains(Object)public boolean retainAll(java.util.Collection<?> c)
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.retainAll in interface java.util.Collection<E>retainAll in interface java.util.List<E>c - the collection containing the elements to be retained by this
list.List.remove(Object),
List.contains(Object)public void clear()
public boolean addAll(java.util.Collection<? extends E> c)
addAll in interface java.util.Collection<E>addAll in interface java.util.List<E>c - a Collection containing elements to be added to this Listjava.lang.NullPointerException - if c is nullList.add(Object)public boolean addAll(int index, java.util.Collection<? extends E> c)
addAll in interface java.util.List<E>index - the index at which the elements will be insertedc - the Collection containing the elements to be insertedjava.lang.IndexOutOfBoundsException - if index < 0 || index > 0java.lang.NullPointerException - if c is nullList.add(int, Object)public boolean addIfAbsent(E val)
val - the element to add to the list.public int addAllAbsent(java.util.Collection<? extends E> c)
c - the Collection containing the elements to be insertedpublic java.lang.String toString()
java.lang.ObjectSystem.out.println()
and such.
It is typical, but not required, to ensure that this method
never completes abruptly with a RuntimeException.
This method will be called when performing string
concatenation with this object. If the result is
null, string concatenation will instead
use "null".
The default implementation returns
getClass().getName() + "@" +
Integer.toHexString(hashCode()).
toString in class java.lang.ObjectObject.getClass(),
Object.hashCode(),
Class.getName(),
Integer.toHexString(int)public boolean equals(java.lang.Object o)
java.lang.ObjectThere are some fairly strict requirements on this
method which subclasses must follow:
a.equals(b) and
b.equals(c), then a.equals(c)
must be true as well.a.equals(b) and
b.equals(a) must have the same value.a.equals(a) must
always be true.a.equals(null) must be false.a.equals(b) must imply
a.hashCode() == b.hashCode().
The reverse is not true; two objects that are not
equal may have the same hashcode, but that has
the potential to harm hashing performance.This is typically overridden to throw a ClassCastException
if the argument is not comparable to the class performing
the comparison, but that is not a requirement. It is legal
for a.equals(b) to be true even though
a.getClass() != b.getClass(). Also, it
is typical to never cause a NullPointerException.
In general, the Collections API (java.util) use the
equals method rather than the ==
operator to compare objects. However, IdentityHashMap
is an exception to this rule, for its own good reasons.
The default implementation returns this == o.
public int hashCode()
java.lang.ObjectThere are some requirements on this method which
subclasses must follow:
a.equals(b) is true, then
a.hashCode() == b.hashCode() must be as well.
However, the reverse is not necessarily true, and two
objects may have the same hashcode without being equal.Notice that since hashCode is used in
Hashtable and other hashing classes,
a poor implementation will degrade the performance of hashing
(so don't blindly implement it as returning a constant!). Also,
if calculating the hash is time-consuming, a class may consider
caching the results.
The default implementation returns
System.identityHashCode(this)
public java.util.Iterator<E> iterator()
public java.util.ListIterator<E> listIterator()
listIterator in interface java.util.List<E>public java.util.ListIterator<E> listIterator(int index)
next() will thus
return the element at index, while an initial call to
previous() will return the element at index-1. The
Iterator uses a snapshot of the state of the internal storage
at the moment this method is called and does not support
update operations, so no synchronization is needed to traverse the
iterator.listIterator in interface java.util.List<E>index - the index at which to start iterating.public java.util.List<E> subList(int fromIndex, int toIndex)
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.
subList in interface java.util.List<E>fromIndex - the index that the returned list should start from
(inclusive)toIndex - the index that the returned list should go to (exclusive)java.lang.IndexOutOfBoundsException - if fromIndex < 0
|| toIndex > size()java.lang.IndexOutOfBoundsException - if fromIndex > toIndexConcurrentModificationException,
RandomAccess