Basics of CopyOnWriteArrayList in Java

CopyOnWriteArrayList in java is a thread-safe implementation of the List interface. It is part of the java.util.concurrent package.

CopyOnWriteArrayList in Java

How CopyOnWriteArrayList in Java works internally

CopyOnWriteArrayList as the name suggests, makes a copy of the underlying array whenever a add, remove or update operation has to be performed on the List. This ensures that we can iterate the List without any exceptions even if other threads modify the content of the List.
CopyOnWriteArrayList uses a “snapshot” when we call the iterator method and returns the current state of the array for iteration.The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Also changes or operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

Constructors

CopyOnWriteArrayList()
Creates an empty list.
CopyOnWriteArrayList(Collection c)
Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
CopyOnWriteArrayList(E[] toCopyIn)
Creates a list holding a copy of the given array.

When to use CopyOnWriteArrayList

As mentioned above, when you try to add/remove/update on CopyOnWriteArrayList, it creates a copy of array. This has a performance impact, hence When you want to iterate elements more often and have very less amount of add/remove operations, then go for CopyOnWriteArrayList.

Methods (listed from the java docs)

1. boolean add(E e)
adds the specified element to the end of this list.
2. void add(int index, E element)
adds the specified element at the specified position in this list.
3. boolean addAll(Collection c)
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.
4. boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
5. int addAllAbsent(Collection c)
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.
6. boolean addIfAbsent(E e)
Append the element if not present.
7. void clear()
Removes all of the elements from this list.
8. Object clone()
Returns a shallow copy of this list.
9. boolean contains(Object o)
Returns true if this list contains the specified element.
10.boolean containsAll(Collection c)
Returns true if this list contains all of the elements of the specified collection.
11. boolean equals(Object o)
Compares the specified object with this list for equality.
12. E get(int index)
Returns the element at the specified position in this list.
13. int hashCode()
Returns the hash code value for this list.
14. int indexOf(E e, int index)
Returns the index of the first occurrence of the specified element in this list, searching forwards from index, or returns -1 if the element is not found.
15. int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
16. boolean isEmpty()
Returns true if this list contains no elements.
17. Iterator iterator()
Returns an iterator over the elements in this list in proper sequence.
18. int lastIndexOf(E e, int index)
Returns the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.
19. int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
20. ListIterator listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
21. ListIterator listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
22. E remove(int index)
Removes the element at the specified position in this list.
23. boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
24. boolean removeAll(Collection c)
Removes from this list all of its elements that are contained in the specified collection.
25. boolean retainAll(Collection c)
Retains only the elements in this list that are contained in the specified collection.
26. E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
27. int size()
Returns the number of elements in this list.
28. List subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
29. Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
30. T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
31. String toString()
Returns a string representation of this list.

Reference

1. CopyOnWriteArrayList in Java docs

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn