1 *java.util.concurrent.CopyOnWriteArrayList* *CopyOnWriteArrayList* A thread-safe
3 public class CopyOnWriteArrayList<E>
4 extends |java.lang.Object|
5 implements |java.util.List|
6 |java.util.RandomAccess|
10 |java.util.concurrent.CopyOnWriteArrayList_Description|
11 |java.util.concurrent.CopyOnWriteArrayList_Fields|
12 |java.util.concurrent.CopyOnWriteArrayList_Constructors|
13 |java.util.concurrent.CopyOnWriteArrayList_Methods|
15 ================================================================================
17 *java.util.concurrent.CopyOnWriteArrayList_Constructors*
18 |java.util.concurrent.CopyOnWriteArrayList()|Creates an empty list.
19 |java.util.concurrent.CopyOnWriteArrayList(Collection<?extendsE>)|Creates a lis
20 |java.util.concurrent.CopyOnWriteArrayList(E[])|Creates a list holding a copy o
22 *java.util.concurrent.CopyOnWriteArrayList_Methods*
23 |java.util.concurrent.CopyOnWriteArrayList.add(E)|Appends the specified element
24 |java.util.concurrent.CopyOnWriteArrayList.add(int,E)|Inserts the specified ele
25 |java.util.concurrent.CopyOnWriteArrayList.addAll(Collection<?extendsE>)|Append
26 |java.util.concurrent.CopyOnWriteArrayList.addAll(int,Collection<?extendsE>)|In
27 |java.util.concurrent.CopyOnWriteArrayList.addAllAbsent(Collection<?extendsE>)|
28 |java.util.concurrent.CopyOnWriteArrayList.addIfAbsent(E)|Append the element if
29 |java.util.concurrent.CopyOnWriteArrayList.clear()|Removes all of the elements
30 |java.util.concurrent.CopyOnWriteArrayList.clone()|Returns a shallow copy of th
31 |java.util.concurrent.CopyOnWriteArrayList.contains(Object)|Returns true if thi
32 |java.util.concurrent.CopyOnWriteArrayList.containsAll(Collection<?>)|Returns t
33 |java.util.concurrent.CopyOnWriteArrayList.equals(Object)|Compares the specifie
34 |java.util.concurrent.CopyOnWriteArrayList.get(int)|
35 |java.util.concurrent.CopyOnWriteArrayList.hashCode()|Returns the hash code val
36 |java.util.concurrent.CopyOnWriteArrayList.indexOf(E,int)|Returns the index of
37 |java.util.concurrent.CopyOnWriteArrayList.indexOf(Object)|
38 |java.util.concurrent.CopyOnWriteArrayList.isEmpty()|Returns true if this list
39 |java.util.concurrent.CopyOnWriteArrayList.iterator()|Returns an iterator over
40 |java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(E,int)|Returns the index
41 |java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(Object)|
42 |java.util.concurrent.CopyOnWriteArrayList.listIterator()|
43 |java.util.concurrent.CopyOnWriteArrayList.listIterator(int)|
44 |java.util.concurrent.CopyOnWriteArrayList.remove(int)|Removes the element at t
45 |java.util.concurrent.CopyOnWriteArrayList.remove(Object)|Removes the first occ
46 |java.util.concurrent.CopyOnWriteArrayList.removeAll(Collection<?>)|Removes fro
47 |java.util.concurrent.CopyOnWriteArrayList.retainAll(Collection<?>)|Retains onl
48 |java.util.concurrent.CopyOnWriteArrayList.set(int,E)|Replaces the element at t
49 |java.util.concurrent.CopyOnWriteArrayList.size()|Returns the number of element
50 |java.util.concurrent.CopyOnWriteArrayList.subList(int,int)|Returns a view of t
51 |java.util.concurrent.CopyOnWriteArrayList.toArray()|Returns an array containin
52 |java.util.concurrent.CopyOnWriteArrayList.toArray(T[])|Returns an array contai
53 |java.util.concurrent.CopyOnWriteArrayList.toString()|Returns a string represen
55 *java.util.concurrent.CopyOnWriteArrayList_Description*
57 A thread-safe variant of (|java.util.ArrayList|) in which all mutative
58 operations (add, set, and so on) are implemented by making a fresh copy of the
61 This is ordinarily too costly, but may be more efficient than alternatives when
62 traversal operations vastly outnumber mutations, and is useful when you cannot
63 or don't want to synchronize traversals, yet need to preclude interference
64 among concurrent threads. The "snapshot" style iterator method uses a reference
65 to the state of the array at the point that the iterator was created. This
66 array never changes during the lifetime of the iterator, so interference is
67 impossible and the iterator is guaranteed not to throw
68 ConcurrentModificationException. The iterator will not reflect additions,
69 removals, or changes to the list since the iterator was created.
70 Element-changing operations on iterators themselves (remove, set, and add) are
71 not supported. These methods throw UnsupportedOperationException.
73 All elements are permitted, including null.
75 Memory consistency effects: As with other concurrent collections, actions in a
76 thread prior to placing an object into aCopyOnWriteArrayListhappen-before
77 actions subsequent to the access or removal of that element from
78 theCopyOnWriteArrayListin another thread.
80 This class is a member of the <a
81 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
85 *java.util.concurrent.CopyOnWriteArrayList()*
87 public CopyOnWriteArrayList()
89 Creates an empty list.
92 *java.util.concurrent.CopyOnWriteArrayList(Collection<?extendsE>)*
94 public CopyOnWriteArrayList(java.util.Collection<? extends E> c)
96 Creates a list containing the elements of the specified collection, in the
97 order they are returned by the collection's iterator.
99 c - the collection of initially held elements
101 *java.util.concurrent.CopyOnWriteArrayList(E[])*
103 public CopyOnWriteArrayList(E[] toCopyIn)
105 Creates a list holding a copy of the given array.
107 toCopyIn - the array (a copy of this array is used as the internal array)
109 *java.util.concurrent.CopyOnWriteArrayList.add(E)*
111 public boolean add(E e)
113 Appends the specified element to the end of this list.
116 e - element to be appended to this list
118 Returns: true (as specified by {@link Collection#add})
120 *java.util.concurrent.CopyOnWriteArrayList.add(int,E)*
126 Inserts the specified element at the specified position in this list. Shifts
127 the element currently at that position (if any) and any subsequent elements to
128 the right (adds one to their indices).
132 *java.util.concurrent.CopyOnWriteArrayList.addAll(Collection<?extendsE>)*
134 public boolean addAll(java.util.Collection<? extends E> c)
136 Appends all of the elements in the specified collection to the end of this
137 list, in the order that they are returned by the specified collection's
141 c - collection containing elements to be added to this list
143 Returns: true if this list changed as a result of the call
145 *java.util.concurrent.CopyOnWriteArrayList.addAll(int,Collection<?extendsE>)*
147 public boolean addAll(
149 java.util.Collection<? extends E> c)
151 Inserts all of the elements in the specified collection into this list,
152 starting at the specified position. Shifts the element currently at that
153 position (if any) and any subsequent elements to the right (increases their
154 indices). The new elements will appear in this list in the order that they are
155 returned by the specified collection's iterator.
158 index - index at which to insert the first element from the specified collection
159 c - collection containing elements to be added to this list
161 Returns: true if this list changed as a result of the call
163 *java.util.concurrent.CopyOnWriteArrayList.addAllAbsent(Collection<?extendsE>)*
165 public int addAllAbsent(java.util.Collection<? extends E> c)
167 Appends all of the elements in the specified collection that are not already
168 contained in this list, to the end of this list, in the order that they are
169 returned by the specified collection's iterator.
172 c - collection containing elements to be added to this list
174 Returns: the number of elements added
176 *java.util.concurrent.CopyOnWriteArrayList.addIfAbsent(E)*
178 public boolean addIfAbsent(E e)
180 Append the element if not present.
183 e - element to be added to this list, if absent
185 Returns: true if the element was added
187 *java.util.concurrent.CopyOnWriteArrayList.clear()*
191 Removes all of the elements from this list. The list will be empty after this
196 *java.util.concurrent.CopyOnWriteArrayList.clone()*
198 public |java.lang.Object| clone()
200 Returns a shallow copy of this list. (The elements themselves are not copied.)
204 Returns: a clone of this list
206 *java.util.concurrent.CopyOnWriteArrayList.contains(Object)*
208 public boolean contains(java.lang.Object o)
210 Returns true if this list contains the specified element. More formally,
211 returns true if and only if this list contains at least one element e such that
212 (o==null?e==null:o.equals(e)).
215 o - element whose presence in this list is to be tested
217 Returns: true if this list contains the specified element
219 *java.util.concurrent.CopyOnWriteArrayList.containsAll(Collection<?>)*
221 public boolean containsAll(java.util.Collection<?> c)
223 Returns true if this list contains all of the elements of the specified
227 c - collection to be checked for containment in this list
229 Returns: true if this list contains all of the elements of the specified collection
231 *java.util.concurrent.CopyOnWriteArrayList.equals(Object)*
233 public boolean equals(java.lang.Object o)
235 Compares the specified object with this list for equality. Returnstrueif the
236 specified object is the same object as this object, or if it is also a
237 (|java.util.List|) and the sequence of elements returned by an
238 iterator(|java.util.List|) over the specified list is the same as the sequence
239 returned by an iterator over this list. The two sequences are considered to be
240 the same if they have the same length and corresponding elements at the same
241 position in the sequence are equal. Two elementse1ande2are considered equal
242 if(e1==null ? e2==null : e1.equals(e2)).
245 o - the object to be compared for equality with this list
247 Returns: {@code true} if the specified object is equal to this list
249 *java.util.concurrent.CopyOnWriteArrayList.get(int)*
251 public |E| get(int index)
257 *java.util.concurrent.CopyOnWriteArrayList.hashCode()*
259 public int hashCode()
261 Returns the hash code value for this list.
263 This implementation uses the definition in (|java.util.List|) .
267 Returns: the hash code value for this list
269 *java.util.concurrent.CopyOnWriteArrayList.indexOf(E,int)*
275 Returns the index of the first occurrence of the specified element in this
276 list, searching forwards from index, or returns -1 if the element is not found.
277 More formally, returns the lowest index i such that (i>=index and and
278 (e==null?get(i)==null:e.equals(get(i)))), or -1 if there is no such index.
281 e - element to search for
282 index - index to start searching from
284 Returns: the index of the first occurrence of the element in this list at position index
285 or later in the list; -1 if the element is not found.
287 *java.util.concurrent.CopyOnWriteArrayList.indexOf(Object)*
289 public int indexOf(java.lang.Object o)
295 *java.util.concurrent.CopyOnWriteArrayList.isEmpty()*
297 public boolean isEmpty()
299 Returns true if this list contains no elements.
303 Returns: true if this list contains no elements
305 *java.util.concurrent.CopyOnWriteArrayList.iterator()*
307 public |java.util.Iterator|<E> iterator()
309 Returns an iterator over the elements in this list in proper sequence.
311 The returned iterator provides a snapshot of the state of the list when the
312 iterator was constructed. No synchronization is needed while traversing the
313 iterator. The iterator does NOT support the remove method.
317 Returns: an iterator over the elements in this list in proper sequence
319 *java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(E,int)*
321 public int lastIndexOf(
325 Returns the index of the last occurrence of the specified element in this list,
326 searching backwards from index, or returns -1 if the element is not found. More
327 formally, returns the highest index i such that (i<=index and and
328 (e==null?get(i)==null:e.equals(get(i)))), or -1 if there is no such index.
331 e - element to search for
332 index - index to start searching backwards from
334 Returns: the index of the last occurrence of the element at position less than or equal
335 to index in this list; -1 if the element is not found.
337 *java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(Object)*
339 public int lastIndexOf(java.lang.Object o)
345 *java.util.concurrent.CopyOnWriteArrayList.listIterator()*
347 public |java.util.ListIterator|<E> listIterator()
349 The returned iterator provides a snapshot of the state of the list when the
350 iterator was constructed. No synchronization is needed while traversing the
351 iterator. The iterator does NOT support the remove, set or add methods.
355 *java.util.concurrent.CopyOnWriteArrayList.listIterator(int)*
357 public |java.util.ListIterator|<E> listIterator(int index)
359 The returned iterator provides a snapshot of the state of the list when the
360 iterator was constructed. No synchronization is needed while traversing the
361 iterator. The iterator does NOT support the remove, set or add methods.
365 *java.util.concurrent.CopyOnWriteArrayList.remove(int)*
367 public |E| remove(int index)
369 Removes the element at the specified position in this list. Shifts any
370 subsequent elements to the left (subtracts one from their indices). Returns the
371 element that was removed from the list.
375 *java.util.concurrent.CopyOnWriteArrayList.remove(Object)*
377 public boolean remove(java.lang.Object o)
379 Removes the first occurrence of the specified element from this list, if it is
380 present. If this list does not contain the element, it is unchanged. More
381 formally, removes the element with the lowest index i such that
382 (o==null?get(i)==null:o.equals(get(i))) (if such an element exists). Returns
383 true if this list contained the specified element (or equivalently, if this
384 list changed as a result of the call).
387 o - element to be removed from this list, if present
389 Returns: true if this list contained the specified element
391 *java.util.concurrent.CopyOnWriteArrayList.removeAll(Collection<?>)*
393 public boolean removeAll(java.util.Collection<?> c)
395 Removes from this list all of its elements that are contained in the specified
396 collection. This is a particularly expensive operation in this class because of
397 the need for an internal temporary array.
400 c - collection containing elements to be removed from this list
402 Returns: true if this list changed as a result of the call
404 *java.util.concurrent.CopyOnWriteArrayList.retainAll(Collection<?>)*
406 public boolean retainAll(java.util.Collection<?> c)
408 Retains only the elements in this list that are contained in the specified
409 collection. In other words, removes from this list all of its elements that are
410 not contained in the specified collection.
413 c - collection containing elements to be retained in this list
415 Returns: true if this list changed as a result of the call
417 *java.util.concurrent.CopyOnWriteArrayList.set(int,E)*
423 Replaces the element at the specified position in this list with the specified
428 *java.util.concurrent.CopyOnWriteArrayList.size()*
432 Returns the number of elements in this list.
436 Returns: the number of elements in this list
438 *java.util.concurrent.CopyOnWriteArrayList.subList(int,int)*
440 public |java.util.List|<E> subList(
444 Returns a view of the portion of this list between fromIndex, inclusive, and
445 toIndex, exclusive. The returned list is backed by this list, so changes in the
446 returned list are reflected in this list, and vice-versa. While mutative
447 operations are supported, they are probably not very useful for
448 CopyOnWriteArrayLists.
450 The semantics of the list returned by this method become undefined if the
451 backing list (i.e., this list) is structurally modified in any way other than
452 via the returned list. (Structural modifications are those that change the size
453 of the list, or otherwise perturb it in such a fashion that iterations in
454 progress may yield incorrect results.)
457 fromIndex - low endpoint (inclusive) of the subList
458 toIndex - high endpoint (exclusive) of the subList
460 Returns: a view of the specified range within this list
462 *java.util.concurrent.CopyOnWriteArrayList.toArray()*
464 public |java.lang.Object|[] toArray()
466 Returns an array containing all of the elements in this list in proper sequence
467 (from first to last element).
469 The returned array will be "safe" in that no references to it are maintained by
470 this list. (In other words, this method must allocate a new array). The caller
471 is thus free to modify the returned array.
473 This method acts as bridge between array-based and collection-based APIs.
477 Returns: an array containing all the elements in this list
479 *java.util.concurrent.CopyOnWriteArrayList.toArray(T[])*
481 public |T|[] toArray(T[] a)
483 Returns an array containing all of the elements in this list in proper sequence
484 (from first to last element); the runtime type of the returned array is that of
485 the specified array. If the list fits in the specified array, it is returned
486 therein. Otherwise, a new array is allocated with the runtime type of the
487 specified array and the size of this list.
489 If this list fits in the specified array with room to spare (i.e., the array
490 has more elements than this list), the element in the array immediately
491 following the end of the list is set to null. (This is useful in determining
492 the length of this list only if the caller knows that this list does not
493 contain any null elements.)
495 Like the (|java.util.concurrent.CopyOnWriteArrayList|) method, this method acts
496 as bridge between array-based and collection-based APIs. Further, this method
497 allows precise control over the runtime type of the output array, and may,
498 under certain circumstances, be used to save allocation costs.
500 Suppose x is a list known to contain only strings. The following code can be
501 used to dump the list into a newly allocated array of String:
505 String[] y = x.toArray(new String[0]);
507 Note that toArray(new Object[0]) is identical in function to toArray().
510 a - the array into which the elements of the list are to be stored, if it is big
511 enough; otherwise, a new array of the same runtime type is allocated for
514 Returns: an array containing all the elements in this list
516 *java.util.concurrent.CopyOnWriteArrayList.toString()*
518 public |java.lang.String| toString()
520 Returns a string representation of this list. The string representation
521 consists of the string representations of the list's elements in the order they
522 are returned by its iterator, enclosed in square brackets ("[]"). Adjacent
523 elements are separated by the characters ", " (comma and space). Elements are
524 converted to strings as by (|java.lang.String|) .
528 Returns: a string representation of this list