Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class CopyOnWriteArrayList<E>

  1. Object
  2. CopyOnWriteArrayList

ImplementsCloneable, Collection<E>, Iterable<E>, List<E>, RandomAccess, Serializable

java.util.concurrent.CopyOnWriteArrayList for the Codename One runtime.

Every mutation replaces the backing array with a modified copy, and an iterator keeps the array that was current when it was created. That is the property listener lists rely on, and it matters on a single thread as much as across threads: a listener that removes itself, or adds another, while the list is being walked to deliver an event neither disturbs the walk nor throws ConcurrentModificationException.

It is safe to use from several threads at once: mutations are made one at a time under an internal lock, and reads take the current array.

Iterators are read only, as in the JDK: remove, set and add on one throw UnsupportedOperationException. subList differs from the JDK: it returns an unmodifiable copy of the range instead of a live view.

Constructors

public CopyOnWriteArrayList()
public CopyOnWriteArrayList(Collection<? extends E> c)
public CopyOnWriteArrayList(E[] toCopyIn)

Methods

public int size()Returns the number of elements in this List.
public boolean isEmpty()Returns whether this List contains no elements.
public boolean contains(Object o)Tests whether this List contains the specified object.
public int indexOf(Object o)Searches this List for the specified object and returns the index of the first occurrence.
public int indexOf(E e, int index)
public int lastIndexOf(Object o)Searches this List for the specified object and returns the index of the last occurrence.
public int lastIndexOf(E e, int index)
public Object clone()A list sharing this one’s current array.
public Object[] toArray()Returns an array containing all elements contained in this List.
public <T> T[] toArray(T[] a)Returns an array containing all elements contained in this List.
public E get(int index)Returns the element at the specified location in this List.
public E set(int index, E element)Replaces the element at the specified location in this List with the specified object.
public boolean add(E e)Adds the specified object at the end of this List.
public void add(int index, E element)Inserts the specified object into this List at the specified location.
public E remove(int index)Removes the object at the specified location from this List.
public boolean remove(Object o)Removes the first occurrence of the specified object from this List.
public boolean addIfAbsent(E e)
public boolean containsAll(Collection<?> c)Tests whether this List contains all objects contained in the specified collection.
public boolean removeAll(Collection<?> c)Removes all occurrences in this List of each object in the specified collection.
public boolean retainAll(Collection<?> c)Removes all objects from this List that are not contained in the specified collection.
public int addAllAbsent(Collection<? extends E> c)
public void clear()Removes all elements from this List, leaving it empty.
public boolean addAll(Collection<? extends E> c)Adds the objects in the specified collection to the end of this List.
public boolean addAll(int index, Collection<? extends E> c)Inserts the objects in the specified collection at the specified location in this List.
public void forEach(Consumer<? super E> action)Performs the given action for each element of the Iterable.
public boolean removeIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
public void replaceAll(UnaryOperator<E> operator)Replaces each element of this list with the result of applying the operator to that element.
public void sort(Comparator<? super E> c)Sorts this list according to the order induced by the specified Comparator.
public String toString()Returns a string representation of the object.
public boolean equals(Object o)Indicates whether some other object is “equal to” this one.
public int hashCode()Returns a hash code value for the object.
public Iterator<E> iterator()Returns an iterator on the elements of this List.
public ListIterator<E> listIterator()Returns a List iterator on the elements of this List.
public ListIterator<E> listIterator(int index)Returns a list iterator on the elements of this List.
public List<E> subList(int fromIndex, int toIndex)An unmodifiable copy of the range.

Inherited methods

Constructor details

CopyOnWriteArrayList

public CopyOnWriteArrayList()

CopyOnWriteArrayList

public CopyOnWriteArrayList(Collection<? extends E> c)

CopyOnWriteArrayList

public CopyOnWriteArrayList(E[] toCopyIn)

Method details

size

public int size()
Returns the number of elements in this List.

Returns

the number of elements in this List.

isEmpty

public boolean isEmpty()
Returns whether this List contains no elements.

Returns

true if this List has no elements, false otherwise.

See also

contains

public boolean contains(Object o)
Tests whether this List contains the specified object.

Parameters

o Object
the object to search for.

Returns

true if object is an element of this List, false otherwise

indexOf

public int indexOf(Object o)
Searches this List for the specified object and returns the index of the first occurrence.

Parameters

o Object
the object to search for.

Returns

the index of the first occurrence of the object or -1 if the object was not found.

indexOf

public int indexOf(E e, int index)

lastIndexOf

public int lastIndexOf(Object o)
Searches this List for the specified object and returns the index of the last occurrence.

Parameters

o Object
the object to search for.

Returns

the index of the last occurrence of the object, or -1 if the object was not found.

lastIndexOf

public int lastIndexOf(E e, int index)

clone

public Object clone()
A list sharing this one’s current array. Neither list ever writes to an array it has published, so the two are independent from here on, which is all a copy has to be.

toArray

public Object[] toArray()
Returns an array containing all elements contained in this List.

Returns

an array of the elements from this List.

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all elements contained in this List. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this List, the array element following the collection elements is set to null.

Parameters

a T[]
the array.

Returns

an array of the elements from this List.

Throws

ArrayStoreException
if the type of an element in this List cannot be stored in the type of the specified array.

get

public E get(int index)
Returns the element at the specified location in this List.

Parameters

index int
the index of the element to return.

Returns

the element at the specified location.

Throws

IndexOutOfBoundsException
if location = size()

set

public E set(int index, E element)
Replaces the element at the specified location in this List with the specified object. This operation does not change the size of the List.

Parameters

index int
the index at which to put the specified object.
element E
the object to insert.

Returns

the previous element at the index.

Throws

UnsupportedOperationException
if replacing elements in this List is not supported.
ClassCastException
if the class of an object is inappropriate for this List.
IllegalArgumentException
if an object cannot be added to this List.
IndexOutOfBoundsException
if location = size()

add

public boolean add(E e)
Adds the specified object at the end of this List.

Parameters

e E
the object to add.

Returns

always true.

Throws

UnsupportedOperationException
if adding to this List is not supported.
ClassCastException
if the class of the object is inappropriate for this List.
IllegalArgumentException
if the object cannot be added to this List.

add

public void add(int index, E element)
Inserts the specified object into this List at the specified location. The object is inserted before the current element at the specified location. If the location is equal to the size of this List, the object is added at the end. If the location is smaller than the size of this List, then all elements beyond the specified location are moved by one position towards the end of the List.

Parameters

index int
the index at which to insert.
element E
the object to add.

Throws

UnsupportedOperationException
if adding to this List is not supported.
ClassCastException
if the class of the object is inappropriate for this List.
IllegalArgumentException
if the object cannot be added to this List.
IndexOutOfBoundsException
if location size()

remove

public E remove(int index)
Removes the object at the specified location from this List.

Parameters

index int
the index of the object to remove.

Returns

the removed object.

Throws

UnsupportedOperationException
if removing from this List is not supported.
IndexOutOfBoundsException
if location = size()

remove

public boolean remove(Object o)
Removes the first occurrence of the specified object from this List.

Parameters

o Object
the object to remove.

Returns

true if this List was modified by this operation, false otherwise.

Throws

UnsupportedOperationException
if removing from this List is not supported.

addIfAbsent

public boolean addIfAbsent(E e)

containsAll

public boolean containsAll(Collection<?> c)
Tests whether this List contains all objects contained in the specified collection.

Parameters

c Collection<?>
the collection of objects

Returns

true if all objects in the specified collection are elements of this List, false otherwise.

removeAll

public boolean removeAll(Collection<?> c)
Removes all occurrences in this List of each object in the specified collection.

Parameters

c Collection<?>
the collection of objects to remove.

Returns

true if this List is modified, false otherwise.

Throws

UnsupportedOperationException
if removing from this List is not supported.

retainAll

public boolean retainAll(Collection<?> c)
Removes all objects from this List that are not contained in the specified collection.

Parameters

c Collection<?>
the collection of objects to retain.

Returns

true if this List is modified, false otherwise.

Throws

UnsupportedOperationException
if removing from this List is not supported.

addAllAbsent

public int addAllAbsent(Collection<? extends E> c)

clear

public void clear()
Removes all elements from this List, leaving it empty.

Throws

UnsupportedOperationException
if removing from this List is not supported.

addAll

public boolean addAll(Collection<? extends E> c)
Adds the objects in the specified collection to the end of this List. The objects are added in the order in which they are returned from the collection’s iterator.

Parameters

c Collection<? extends E>
the collection of objects.

Returns

true if this List is modified, false otherwise (i.e. if the passed collection was empty).

Throws

UnsupportedOperationException
if adding to this List is not supported.
ClassCastException
if the class of an object is inappropriate for this List.
IllegalArgumentException
if an object cannot be added to this List.

addAll

public boolean addAll(int index, Collection<? extends E> c)
Inserts the objects in the specified collection at the specified location in this List. The objects are added in the order they are returned from the collection’s iterator.

Parameters

index int
the index at which to insert.
c Collection<? extends E>
the collection of objects to be inserted.

Returns

true if this List has been modified through the insertion, false otherwise (i.e. if the passed collection was empty).

Throws

UnsupportedOperationException
if adding to this List is not supported.
ClassCastException
if the class of an object is inappropriate for this List.
IllegalArgumentException
if an object cannot be added to this List.
IndexOutOfBoundsException
if location size()

forEach

public void forEach(Consumer<? super E> action)
Performs the given action for each element of the Iterable. Stubbed in the CLDC11 subset; the actual implementation comes from the platform’s JDK at runtime.

removeIf

public boolean removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate. Stubbed in the CLDC11 subset; the actual implementation comes from the platform’s JDK at runtime.

replaceAll

public void replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element. Stubbed in the CLDC11 subset; the actual implementation comes from the platform’s JDK at runtime.

sort

public void sort(Comparator<? super E> c)
Sorts this list according to the order induced by the specified Comparator. Stubbed in the CLDC11 subset; the actual implementation comes from the platform’s JDK at runtime.

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

equals

public boolean equals(Object o)
Indicates whether some other object is “equal to” this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

iterator

public Iterator<E> iterator()
Returns an iterator on the elements of this List. The elements are iterated in the same order as they occur in the List.

Returns

an iterator on the elements of this List.

See also

listIterator

public ListIterator<E> listIterator()
Returns a List iterator on the elements of this List. The elements are iterated in the same order that they occur in the List.

Returns

a List iterator on the elements of this List

See also

listIterator

public ListIterator<E> listIterator(int index)
Returns a list iterator on the elements of this List. The elements are iterated in the same order as they occur in the List. The iteration starts at the specified location.

Parameters

index int
the index at which to start the iteration.

Returns

a list iterator on the elements of this List.

Throws

IndexOutOfBoundsException
if location size()

See also

subList

public List<E> subList(int fromIndex, int toIndex)
An unmodifiable copy of the range. The JDK returns a live view; code that writes through a sublist of a copy-on-write list gets an UnsupportedOperationException here instead of a lost update.

Parameters

fromIndex int
the index at which to start the sublist.
toIndex int
the index one past the end of the sublist.

Returns

a list of a portion of this List.

Throws

IndexOutOfBoundsException
if start end or end > size()