Class CopyOnWriteArrayList<E>
- java.lang.Object
-
- java.util.concurrent.CopyOnWriteArrayList<E>
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,List<E>,RandomAccess
public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable
A thread-safe random-access list.Read operations (including
get(int)) do not block and may overlap with update operations. Reads reflect the results of the most recently completed operations. Aggregate operations likeaddAll(java.util.Collection<? extends E>)andclear()are atomic; they never expose an intermediate state.Iterators of this list never throw
ConcurrentModificationException. When an iterator is created, it keeps a copy of the list's contents. It is always safe to iterate this list, but iterations may not reflect the latest state of the list.Iterators returned by this list and its sub lists cannot modify the underlying list. In particular,
Iterator.remove(),ListIterator.add(E)andListIterator.set(E)all throwUnsupportedOperationException.This class offers extended API beyond the
Listinterface. It includes additional overloads for indexed search (indexOf(E, int)andlastIndexOf(E, int)) and methods for conditional adds (addIfAbsent(E)andaddAllAbsent(java.util.Collection<? extends E>)).- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CopyOnWriteArrayList()Creates a new empty instance.CopyOnWriteArrayList(E[] array)Creates a new instance containing the elements ofarray.CopyOnWriteArrayList(Collection<? extends E> collection)Creates a new instance containing the elements ofcollection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E e)Inserts the specified object into thisListat the specified location.booleanadd(E e)Adds the specified object at the end of thisList.booleanaddAll(int index, Collection<? extends E> collection)Inserts the objects in the specified collection at the specified location in thisList.booleanaddAll(Collection<? extends E> collection)Adds the objects in the specified collection to the end of thisList.intaddAllAbsent(Collection<? extends E> collection)Adds the elements ofcollectionthat are not already present in this list.booleanaddIfAbsent(E object)Addsobjectto the end of this list if it is not already present.voidclear()Removes all elements from thisList, leaving it empty.Objectclone()Creates and returns a copy of thisObject.booleancontains(Object o)Tests whether thisListcontains the specified object.booleancontainsAll(Collection<?> collection)Tests whether thisListcontains all objects contained in the specified collection.booleanequals(Object other)Compares this instance with the specified object and indicates if they are equal.Eget(int index)Returns the element at the specified location in thisList.inthashCode()Returns an integer hash code for this object.intindexOf(E object, int from)Searches this list forobjectand returns the index of the first occurrence that is at or afterfrom.intindexOf(Object object)Searches thisListfor the specified object and returns the index of the first occurrence.booleanisEmpty()Returns whether thisListcontains no elements.Iterator<E>iterator()Returns anIteratorthat iterates over the elements of this list as they were at the time of this method call.intlastIndexOf(E object, int to)Searches this list forobjectand returns the index of the last occurrence that is beforeto.intlastIndexOf(Object object)Searches thisListfor the specified object and returns the index of the last occurrence.ListIterator<E>listIterator()Equivalent tolistIterator(0).ListIterator<E>listIterator(int index)Returns aListIteratorthat iterates over the elements of this list as they were at the time of this method call.Eremove(int index)Removes the object at the specified location from thisList.booleanremove(Object o)Removes the first occurrence of the specified object from thisList.booleanremoveAll(Collection<?> collection)Removes all occurrences in thisListof each object in the specified collection.booleanretainAll(Collection<?> collection)Removes all objects from thisListthat are not contained in the specified collection.Eset(int index, E e)Replaces the element at the specified location in thisListwith the specified object.intsize()Returns the number of elements in thisList.List<E>subList(int from, int to)Returns aListof the specified portion of thisListfrom the given start index to the end index minus one.Object[]toArray()Returns an array containing all elements contained in thisList.<T> T[]toArray(T[] contents)Returns an array containing all elements contained in thisList.StringtoString()Returns a string containing a concise, human-readable description of this object.
-
-
-
Constructor Detail
-
CopyOnWriteArrayList
public CopyOnWriteArrayList()
Creates a new empty instance.
-
CopyOnWriteArrayList
public CopyOnWriteArrayList(Collection<? extends E> collection)
Creates a new instance containing the elements ofcollection.
-
CopyOnWriteArrayList
public CopyOnWriteArrayList(E[] array)
Creates a new instance containing the elements ofarray.
-
-
Method Detail
-
clone
public Object clone()
Description copied from class:ObjectCreates and returns a copy of thisObject. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should callsuper.clone()to create the new instance and then create deep copies of the nested, mutable objects.
-
size
public int size()
Description copied from interface:ListReturns the number of elements in thisList.
-
get
public E get(int index)
Description copied from interface:ListReturns the element at the specified location in thisList.
-
contains
public boolean contains(Object o)
Description copied from interface:ListTests whether thisListcontains the specified object.
-
containsAll
public boolean containsAll(Collection<?> collection)
Description copied from interface:ListTests whether thisListcontains all objects contained in the specified collection.- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>- Parameters:
collection- the collection of objects- Returns:
trueif all objects in the specified collection are elements of thisList,falseotherwise.
-
indexOf
public int indexOf(E object, int from)
Searches this list forobjectand returns the index of the first occurrence that is at or afterfrom.- Returns:
- the index or -1 if the object was not found.
-
indexOf
public int indexOf(Object object)
Description copied from interface:ListSearches thisListfor the specified object and returns the index of the first occurrence.
-
lastIndexOf
public int lastIndexOf(E object, int to)
Searches this list forobjectand returns the index of the last occurrence that is beforeto.- Returns:
- the index or -1 if the object was not found.
-
lastIndexOf
public int lastIndexOf(Object object)
Description copied from interface:ListSearches thisListfor the specified object and returns the index of the last occurrence.- Specified by:
lastIndexOfin interfaceList<E>- Parameters:
object- the object to search for.- Returns:
- the index of the last occurrence of the object, or -1 if the object was not found.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ListReturns whether thisListcontains no elements.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Returns:
trueif thisListhas no elements,falseotherwise.- See Also:
List.size()
-
iterator
public Iterator<E> iterator()
Returns anIteratorthat iterates over the elements of this list as they were at the time of this method call. Changes to the list made after this method call will not be reflected by the iterator, nor will they trigger aConcurrentModificationException.The returned iterator does not support
Iterator.remove().
-
listIterator
public ListIterator<E> listIterator(int index)
Returns aListIteratorthat iterates over the elements of this list as they were at the time of this method call. Changes to the list made after this method call will not be reflected by the iterator, nor will they trigger aConcurrentModificationException.The returned iterator does not support
ListIterator.add(E),ListIterator.set(E)orIterator.remove(),- Specified by:
listIteratorin interfaceList<E>- Parameters:
index- the index at which to start the iteration.- Returns:
- a list iterator on the elements of this
List. - See Also:
ListIterator
-
listIterator
public ListIterator<E> listIterator()
Equivalent tolistIterator(0).- Specified by:
listIteratorin interfaceList<E>- Returns:
- a
Listiterator on the elements of thisList - See Also:
ListIterator
-
subList
public List<E> subList(int from, int to)
Description copied from interface:ListReturns aListof the specified portion of thisListfrom the given start index to the end index minus one. The returnedListis backed by thisListso changes to it are reflected by the other.
-
toArray
public Object[] toArray()
Description copied from interface:ListReturns an array containing all elements contained in thisList.
-
toArray
public <T> T[] toArray(T[] contents)
Description copied from interface:ListReturns an array containing all elements contained in thisList. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than thisList, the array element following the collection elements is set to null.
-
equals
public boolean equals(Object other)
Description copied from class:ObjectCompares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classObject- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
add
public boolean add(E e)
Description copied from interface:ListAdds the specified object at the end of thisList.
-
add
public void add(int index, E e)Description copied from interface:ListInserts the specified object into thisListat the specified location. The object is inserted before the current element at the specified location. If the location is equal to the size of thisList, the object is added at the end. If the location is smaller than the size of thisList, then all elements beyond the specified location are moved by one position towards the end of theList.
-
addAll
public boolean addAll(Collection<? extends E> collection)
Description copied from interface:ListAdds the objects in the specified collection to the end of thisList. The objects are added in the order in which they are returned from the collection's iterator.
-
addAll
public boolean addAll(int index, Collection<? extends E> collection)Description copied from interface:ListInserts the objects in the specified collection at the specified location in thisList. The objects are added in the order they are returned from the collection's iterator.
-
addAllAbsent
public int addAllAbsent(Collection<? extends E> collection)
Adds the elements ofcollectionthat are not already present in this list. Ifcollectionincludes a repeated value, at most one occurrence of that value will be added to this list. Elements are added at the end of this list.Callers of this method may prefer
CopyOnWriteArraySet, whose API is more appropriate for set operations.
-
addIfAbsent
public boolean addIfAbsent(E object)
Addsobjectto the end of this list if it is not already present.Callers of this method may prefer
CopyOnWriteArraySet, whose API is more appropriate for set operations.
-
clear
public void clear()
Description copied from interface:ListRemoves all elements from thisList, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- See Also:
List.isEmpty(),List.size()
-
remove
public E remove(int index)
Description copied from interface:ListRemoves the object at the specified location from thisList.
-
remove
public boolean remove(Object o)
Description copied from interface:ListRemoves the first occurrence of the specified object from thisList.
-
removeAll
public boolean removeAll(Collection<?> collection)
Description copied from interface:ListRemoves all occurrences in thisListof each object in the specified collection.
-
retainAll
public boolean retainAll(Collection<?> collection)
Description copied from interface:ListRemoves all objects from thisListthat are not contained in the specified collection.
-
-