Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 778
نمايش فراداده

CopyOnWriteArrayList<E>java.util.concurrent

Java 5.0cloneable serializable collection

This class is a threadsafe java.util.List implementation based on an array. Any number of read operations may proceed concurrently. All update methods are synchronized and make a completely new copy of the internal array, so this class is best suited to applications in which reads greatly outnumber updates. The Iterator of a CopyOnWriteArrayList operates on the copy of the array that was current when the iterator( ) method was called: it does not see any updates that occur after the call to iterator( ) and is guaranteed never to throw ConcurrentModificationException. Update methods of the Iterator and ListIterator interfaces are not supported and throw UnsupportedOperationException.

CopyOnWriteArrayList defines a few useful methods beyond those specified by the List interface. addIfAbsent( ) atomically adds an element to the list, but only if the list does not already contain that element. addAllAbsent( ) adds all elements of a collection that are not already in the list. Two new indexOf( ) and lastIndexOf( ) methods are defined that specify a starting index for the search. These provide a convenient alternative to using a subList( ) view when searching for repeated matches in a list.

Figure 16-78. java.util.concurrent.CopyOnWriteArrayList<E>

public class

CopyOnWriteArrayList<E> implements java.util.List<E>, java.util.RandomAccess, Cloneable, Serializable { // Public Constructors public

CopyOnWriteArrayList ( ); public

CopyOnWriteArrayList (java.util.Collection<? extends E>

c ); public

CopyOnWriteArrayList (E[ ]

toCopyIn ); // Public Instance Methods public int

addAllAbsent (java.util.Collection<? extends E>

c ); synchronized public boolean

addIfAbsent (E

element ); synchronized public int

indexOf (E

elem , int

index ); public int

lastIndexOf (E

elem , int

index ); // Methods Implementing List public boolean

add (E

element ); synchronized public void

add (int

index , E

element ); synchronized public boolean

addAll (java.util.Collection<? extends E>

c ); synchronized public boolean

addAll (int

index , java.util.Collection<? extends E>

c ); synchronized public void

clear ( ); synchronized public boolean

contains (Object

elem ); public boolean

containsAll (java.util.Collection<?>

c ); public boolean

equals (Object

o ); public E

get (int

index ); public int

hashCode ( ); public int

indexOf (Object

elem ); public boolean

isEmpty ( ); default:true public java.util.Iterator<E>

iterator ( ); public int

lastIndexOf (Object

elem ); public java.util.ListIterator<E>

listIterator ( ); public java.util.ListIterator<E>

listIterator (int

index ); public boolean

remove (Object

o ); synchronized public E

remove (int

index ); synchronized public boolean

removeAll (java.util.Collection<?>

c ); synchronized public boolean

retainAll (java.util.Collection<?>

c ); synchronized public E

set (int

index , E

element ); synchronized public int

size ( ); public java.util.List<E>

subList (int

fromIndex , int

toIndex ); synchronized public Object[ ]

toArray ( ); public <T> T[ ]

toArray (T[ ]

a ); // Public Methods Overriding Object public Object

clone ( ); public String

toString ( ); }