CopyOnWriteArrayList
The add , set , remove operations on a given collection result in a new copy of the internal array being created. This ensures that we don't catch ConcurrentModificationException .
Just remember that when copying an array, only references to objects are copied, including access to the fields of non-thread-safe elements. Such collections are best used in cases with a minimum number of write operations.
![](https://cdn.codegym.cc/images/article/dd30e3f5-509c-479f-acb2-47f72f63b032/512.jpeg)
CopyOnWriteArrayList<E> is a thread-safe analogue of ArrayList , implemented on the basis of CopyOnWrite algorithms. Additional methods and constructor are described below:
CopyOnWriteArrayList(E[] toCopyIn) | A constructor that takes an array as input. |
int indexOf(E e, int index) | Returns the index of the first element found, starting at the given index. |
int lastIndexOf(E e, int index) | Returns the index of the first element found in a reverse search, starting at the given index. |
boolean addIfAbsent(E e) | Add an element if it is not in the collection. The equals method is used to compare elements . |
int addAllAbsent(Collection<? extends E> c) | Add elements if they are not in the collection. Returns the number of elements added. |
ConcurrentMap Interface
Improved implementations of HashMap and TreeMap with support for multithreading and scalability.
![](https://cdn.codegym.cc/images/article/b93ad73c-488f-4c46-91c8-9a76c7337e1a/1024.jpeg)
ConcurrentMap<K, V> is an interface that extends Map with additional atomic operations.
V putIfAbsent(K key, V value) | Adds a new key-value pair only if the key is not in the collection. Returns the previous value for the given key. |
boolean remove(Object key, Object value) | Removes the key-value pair only if the given key matches the given value in the Map. Returns true if the element was successfully removed. |
boolean replace(K key, V oldValue, V newValue) | Replaces the old value with the new one by key only if the old value matches the given value in the Map. Returns true if the value has been replaced with a new one. |
V replace(K key, V value) | Replaces an old value with a new one by key only if the key is associated with any value. Returns the previous value for the given key. |
ConcurrentHashMap<K, V> - here the data is presented in the form of segments, which are divided into key hashs. As a result, if you need access, then the segment is locked, not the object. Iterators do not throw ConcurrentModificationException and represent data for a specific period of time.
Implementations of ConcurrentHashMap
ConcurrentHashMap<K, V> - here the data is presented in the form of segments, broken down by key hashs. As a result, data access is locked by segments, not by one object. In addition, iterators represent data for a specific slice of time and do not throw ConcurrentModificationException .
ConcurrentNavigableMap<K,V> - extends the NavigableMap interface and returns a ConcurrentNavigableMap . All iterators are safe to use and do not throw ConcurrentModificationException .
ConcurrentSkipListMap<K, V> is an analogue of TreeMap for multithreading. The data is sorted by key and averaged log(N) performance is guaranteed forcontainsKey,get,put,remove, and other similar operations.
ConcurrentSkipListSet<E> is an implementation of the Set interface based on ConcurrentSkipListMap .