java.util.concurrent
Class CopyOnWriteArrayList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.concurrent.CopyOnWriteArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class CopyOnWriteArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

Since:
1.5
See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
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)
           
 boolean addIfAbsent(E val)
           
 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 iff element is in this ArrayList.
 E get(int index)
          Retrieves the element at the user-supplied index.
 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(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.
 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(Object e)
          Returns the highest index at which element appears in this List, or -1 if it does not appear.
 E remove(int index)
          Removes the element at the user-supplied index.
 E set(int index, E e)
          Sets the element at the specified index.
 int size()
          Returns the number of elements in this list.
 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.
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, remove, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
 

Constructor Detail

CopyOnWriteArrayList

public CopyOnWriteArrayList()
Construct a new ArrayList with the default capacity (16).


CopyOnWriteArrayList

public CopyOnWriteArrayList(Collection<? extends E> c)
Construct a new ArrayList, and initialize it with the elements in the supplied Collection. The initial capacity is 110% of the Collection's size.

Parameters:
c - the collection whose elements will initialize this list
Throws:
NullPointerException - if c is null

CopyOnWriteArrayList

public CopyOnWriteArrayList(E[] array)
Construct a new ArrayList, and initialize it with the elements in the supplied array.

Parameters:
array - the array used to initialize this list
Throws:
NullPointerException - if array is null
Method Detail

size

public int size()
Returns the number of elements in this list.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the list size

isEmpty

public boolean isEmpty()
Checks if the list is empty.

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if there are no elements
See Also:
AbstractCollection.size()

contains

public boolean contains(Object e)
Returns true iff element is in this ArrayList.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
e - the element whose inclusion in the List is being tested
Returns:
true if the list contains e

indexOf

public int indexOf(Object e)
Returns the lowest index at which element appears in this List, or -1 if it does not appear.

Specified by:
indexOf in interface List<E>
Overrides:
indexOf in class AbstractList<E>
Parameters:
e - the element whose inclusion in the List is being tested
Returns:
the index where e was found

indexOf

public 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.

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

lastIndexOf

public int lastIndexOf(Object e)
Returns the highest index at which element appears in this List, or -1 if it does not appear.

Specified by:
lastIndexOf in interface List<E>
Overrides:
lastIndexOf in class AbstractList<E>
Parameters:
e - the element whose inclusion in the List is being tested
Returns:
the index where e was found

lastIndexOf

public 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.

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

clone

public Object clone()
Creates a shallow copy of this ArrayList (elements are not cloned).

Overrides:
clone in class Object
Returns:
the cloned object
See Also:
Cloneable

toArray

public Object[] toArray()
Returns an Object array containing all of the elements in this ArrayList. The array is independent of this list.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array representation of this list

toArray

public <T> T[] toArray(T[] a)
Returns an Array whose component type is the runtime component type of the passed-in Array. The returned Array is populated with all of the elements in this ArrayList. If the passed-in Array is not large enough to store all of the elements in this List, a new Array will be created and returned; if the passed-in Array is larger than the size of this List, then size() index will be set to null.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
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

get

public E get(int index)
Retrieves the element at the user-supplied index.

Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>
Parameters:
index - the index of the element we are fetching
Returns:
the element at that position
Throws:
IndexOutOfBoundsException - if index < 0 || index >= size()

set

public E set(int index,
             E e)
Sets the element at the specified index. The new element, e, can be an object of any type or null.

Specified by:
set in interface List<E>
Overrides:
set in class AbstractList<E>
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

add

public boolean add(E e)
Appends the supplied element to the end of this list. The element, e, can be an object of any type or null.

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
e - the element to be appended to this list
Returns:
true, the add will always succeed
See Also:
AbstractList.add(int, Object)

add

public 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. The element, e, can be an object of any type or null.

Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
index - the index at which the element is being added
e - the item being added
Throws:
IndexOutOfBoundsException - if index < 0 || index > size()
See Also:
AbstractList.modCount

remove

public E remove(int index)
Removes the element at the user-supplied index.

Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractList<E>
Parameters:
index - the index of the element to be removed
Returns:
the removed Object
Throws:
IndexOutOfBoundsException - if index < 0 || index >= size()
See Also:
AbstractList.modCount

clear

public void clear()
Removes all elements from this List

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class AbstractList<E>
See Also:
AbstractList.remove(int), AbstractList.removeRange(int, int)

addAll

public boolean addAll(Collection<? extends E> c)
Add each element in the supplied Collection to this List. It is undefined what happens if you modify the list while this is taking place; for example, if the collection contains this list. c can contain objects of any type, as well as null values.

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>
Overrides:
addAll in class AbstractCollection<E>
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
See Also:
AbstractCollection.add(Object)

addAll

public 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.

Specified by:
addAll in interface List<E>
Overrides:
addAll in class AbstractList<E>
Parameters:
index - the index at which the elements will be inserted
c - the Collection containing the elements to be inserted
Returns:
true if the list was modified by this action, that is, if c is non-empty
Throws:
IndexOutOfBoundsException - if index < 0 || index > 0
NullPointerException - if c is null
See Also:
AbstractList.add(int, Object)

addIfAbsent

public boolean addIfAbsent(E val)

addAllAbsent

public int addAllAbsent(Collection<? extends E> c)