Added in API level 1

CopyOnWriteArrayList

open class CopyOnWriteArrayList<E : Any!> : Cloneable, MutableList<E>, RandomAccess, Serializable
kotlin.Any
   ↳ java.util.concurrent.CopyOnWriteArrayList

A thread-safe variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads. The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

All elements are permitted, including null.

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread.

This class is a member of the Java Collections Framework.

Summary

Public constructors

Creates an empty list.

Creates a list holding a copy of the given array.

Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Public methods
open Boolean
add(element: E)

Appends the specified element to the end of this list.

open Unit
add(index: Int, element: E)

Inserts the specified element at the specified position in this list.

open Boolean
addAll(index: Int, elements: Collection<E>)

Inserts all of the elements in the specified collection into this list, starting at the specified position.

open Boolean
addAll(elements: Collection<E>)

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.

open Int

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.

open Unit
addFirst(e: E)

Adds an element as the first element of this collection (optional operation).

open Boolean

Appends the element, if not present.

open Unit
addLast(e: E)

Adds an element as the last element of this collection (optional operation).

open Unit

Removes all of the elements from this list.

open Any

Returns a shallow copy of this list.

open Boolean
contains(element: E?)

Returns true if this list contains the specified element.

open Boolean
containsAll(elements: Collection<E>)

Returns true if this list contains all of the elements of the specified collection.

open Boolean
equals(other: Any?)

Compares the specified object with this list for equality.

open Unit
forEach(action: Consumer<in E>)

open E
get(index: Int)

Returns the element at the specified position in this list.

open E

Gets the first element of this collection.

open E

Gets the last element of this collection.

open Int

Returns the hash code value for this list.

open Int
indexOf(e: E?, index: Int)

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.

open Int
indexOf(element: E?)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

open Boolean

Returns true if this list contains no elements.

open MutableIterator<E>

Returns an iterator over the elements in this list in proper sequence.

open Int
lastIndexOf(e: E?, index: Int)

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.

open Int
lastIndexOf(element: E?)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

open MutableListIterator<E>

Returns a list iterator over the elements in this list (in proper sequence).

open MutableListIterator<E>
listIterator(index: Int)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

open Boolean
remove(element: E?)

Removes the first occurrence of the specified element from this list, if it is present.

open Boolean
removeAll(elements: Collection<E>)

Removes from this list all of its elements that are contained in the specified collection.

open E
removeAt(index: Int)

Removes the element at the specified position in this list.

open E

Removes and returns the first element of this collection (optional operation).

open Boolean
removeIf(filter: Predicate<in E>)

open E

Removes and returns the last element of this collection (optional operation).

open Unit
replaceAll(operator: UnaryOperator<E>)

Replaces each element of this list with the result of applying the operator to that element (optional operation).

open Boolean
retainAll(elements: Collection<E>)

Retains only the elements in this list that are contained in the specified collection.

open MutableList<E>

Returns a reverse-ordered view of this collection.

open E
set(index: Int, element: E)

Replaces the element at the specified position in this list with the specified element.

open Unit
sort(c: Comparator<in E>?)

Sorts this list according to the order induced by the specified Comparator (optional operation).

open Spliterator<E>

Returns a Spliterator over the elements in this list.

open MutableList<E>
subList(fromIndex: Int, toIndex: Int)

Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.

open Array<Any!>

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

open Array<T>
toArray(a: Array<T>)

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.

open String

Returns a string representation of this list.

Properties
open Int

Returns the number of elements in this list.

Public constructors

CopyOnWriteArrayList

Added in API level 1
CopyOnWriteArrayList()

Creates an empty list.

CopyOnWriteArrayList

Added in API level 1
CopyOnWriteArrayList(toCopyIn: Array<E>)

Creates a list holding a copy of the given array.

Parameters
toCopyIn Array<E>: the array (a copy of this array is used as the internal array)
Exceptions
java.lang.NullPointerException if the specified array is null

CopyOnWriteArrayList

Added in API level 1
CopyOnWriteArrayList(c: MutableCollection<out E>)

Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters
c MutableCollection<out E>: the collection of initially held elements
Exceptions
java.lang.NullPointerException if the specified collection is null

Public methods

add

Added in API level 1
open fun add(element: E): Boolean

Appends the specified element to the end of this list.

Parameters
e element to be appended to this list
Return
Boolean true (as specified by Collection.add)
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list
java.lang.IllegalArgumentException if some property of this element prevents it from being added to this list
java.lang.IllegalStateException if the element cannot be added at this time due to insertion restrictions
java.lang.NullPointerException if the specified element is null and this list does not permit null elements
java.lang.UnsupportedOperationException if the add operation is not supported by this list

add

Added in API level 1
open fun add(
    index: Int,
    element: E
): Unit

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters
index Int: index at which the specified element is to be inserted
element E: element to be inserted
Exceptions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this list
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index > size())
java.lang.NullPointerException if the specified element is null and this list does not permit null elements
java.lang.UnsupportedOperationException if the add operation is not supported by this list

addAll

Added in API level 1
open fun addAll(
    index: Int,
    elements: Collection<E>
): Boolean

Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new