CopyOnWriteArrayList in java
CopyOnWriteArrayList: CopyOnWriteArrayList class is introduced in JDK 1.5, which implements List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy.
Here are few points about CopyOnWriteArrayList:
- As the name indicates, CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation at a certain point both will be synchronized automatically, which is taken care of by JVM. Therefore there is no effect for threads that are performing read operation.
- It is costly to use because for every update operation a cloned copy will be created. Hence CopyOnWriteArrayList is the best choice if our frequent operation is read operation.
- It extends object and implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E> and RandomAccess
- The underlined data structure is a grow-able array.
- It is a thread-safe version of ArrayList.
- Insertion is preserved, duplicates are allowed and heterogeneous Objects are allowed.
- The main important point about CopyOnWriteArrayList is the Iterator of CopyOnWriteArrayList can not perform remove operation otherwise we get Run-time exception saying UnsupportedOperationException.
Constructors Summary:
- CopyOnWriteArrayList c = new CopyOnWriteArrayList(); :Creates an empty list.
- CopyOnWriteArrayList c = new CopyOnWriteArrayList(Collection obj);:Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
- CopyOnWriteArrayList c = new CopyOnWriteArrayList(Object[] obj);:Creates a list holding a copy of the given array.
Methods Summary:
- add(int index, E element): This method Inserts the specified element at the specified position in this list.
- add(E e): This method Appends the specified element to the end of this list.
- addAll(int index, Collection<E> c): This method Inserts all of the elements in the specified collection into this list, starting at the specified position.
- addAll(Collection<E> c): This method Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator.
- addAllAbsent(Collection<E> c): This method Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection’s iterator.
- addIfAbsent(E e): This method Appends the element, if not present.
- clear(): This method Removes all of the elements from this list.
- clone(): This method returns a shallow copy of this list.
- contains(Object o): This method returns true if this list contains the specified element.
- contains(Collection<E> c): This method returns true if this list contains all of the elements of the specified collection.
- equals(Object o): This method Compares the specified object with this list for equality.
- forEach(Consumer<E> action): This method Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
- get(int index): This method returns the element at the specified position in this list.
- hashCode(): This method returns the hash code value for this list.
- indexOf(E e, int index): This method returns the index of the first occurrence of the specified element in this list, searching forwards from the index, or returns -1 if the element is not found.
- indexOf(Object o): This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
- isEmpty(): This method returns true if this list contains no elements.
- iterator(): This method returns an iterator over the elements in this list in a proper sequence.
- lastIndexOf(E e, int index): This method returns the index of the last occurrence of the specified element in this list, searching backward from the index, or returns -1 if the element is not found.
- lastIndexOf(Object o): This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
- list iterator(): This method returns a list iterator over the elements in this list (in proper sequence).
- list iterator(int index): This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
- remove(int index): This method Removes the element at the specified position in this list.
- remove(Object o): This method Removes the first occurrence of the specified element from this list if it is present.
- removeAll(Collection<E> c): This method Removes from this list all of its elements that are contained in the specified collection.
- remove(Predicate<E> filter): This method Removes all of the elements of this collection that satisfy the given predicate.
- replaceAll(UnaryOperator<E> operator): This method Replaces each element of this list with the result of applying the operator to that element.
- retain(Collection<E> c): This method Retains only the elements in this list that are contained in the specified collection.
- set(int index, E element): This method Replaces the element at the specified position in this list with the specified element.
- size(): This method returns the number of elements in this list.
- sort(Comparator<E> c): This method Sorts this list according to the order induced by the specified Comparator.
- spliterator(): This method returns a Spliterator over the elements in this list.
- subList(int fromIndex, int toIndex): This method returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
- toArray(): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element).
- toArray(T[] a): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
- toString(): This method returns a string representation of this list.
filter_none
edit
play_arrow
brightness_4
|
Output:
A B c D [A, B, c, D]
post a comment