Class CopyOnWriteArrayList<E>

java.lang.Object
java.util.concurrent.CopyOnWriteArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, 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.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    CopyOnWriteArrayList(E[] toCopyIn)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, E element)
    Inserts the specified object into this List at the specified location.
    boolean
    add(E e)
    Adds the specified object at the end of this List.
    boolean
    addAll(int index, Collection<? extends E> c)
    Inserts the objects in the specified collection at the specified location in this List.
    boolean
    addAll(Collection<? extends E> c)
    Adds the objects in the specified collection to the end of this List.
    int
    addAllAbsent(Collection<? extends E> c)
     
    boolean
     
    void
    Removes all elements from this List, leaving it empty.
    A list sharing this one's current array.
    boolean
    Tests whether this List contains the specified object.
    boolean
    Tests whether this List contains all objects contained in the specified collection.
    boolean
    Compares the given object with the List, and returns true if they represent the same object using a class specific comparison.
    void
    forEach(Consumer<? super E> action)
    Performs the given action for each element of the Iterable.
    get(int index)
    Returns the element at the specified location in this List.
    int
    Returns the hash code for this List.
    int
    indexOf(E e, int index)
     
    int
    Searches this List for the specified object and returns the index of the first occurrence.
    boolean
    Returns whether this List contains no elements.
    Returns an iterator on the elements of this List.
    int
    lastIndexOf(E e, int index)
     
    int
    Searches this List for the specified object and returns the index of the last occurrence.
    Returns a List iterator on the elements of this List.
    listIterator(int index)
    Returns a list iterator on the elements of this List.
    remove(int index)
    Removes the object at the specified location from this List.
    boolean
    Removes the first occurrence of the specified object from this List.
    boolean
    Removes all occurrences in this List of each object in the specified collection.
    boolean
    removeIf(Predicate<? super E> filter)
    Removes all of the elements of this collection that satisfy the given predicate.
    void
    Replaces each element of this list with the result of applying the operator to that element.
    boolean
    Removes all objects from this List that are not contained in the specified collection.
    set(int index, E element)
    Replaces the element at the specified location in this List with the specified object.
    int
    Returns the number of elements in this List.
    void
    sort(Comparator<? super E> c)
    Sorts this list according to the order induced by the specified Comparator.
    subList(int fromIndex, int toIndex)
    An unmodifiable copy of the range.
    Returns an array containing all elements contained in this List.
    <T> T[]
    toArray(T[] a)
    Returns an array containing all elements contained in this List.
    Returns a string representation of the object.

    Methods inherited from class Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Collection

    parallelStream, stream, toArray

    Methods inherited from interface List

    addFirst, addLast, getFirst, getLast, removeFirst, removeLast, reversed, spliterator
  • Constructor Details

    • CopyOnWriteArrayList

      public CopyOnWriteArrayList()
    • CopyOnWriteArrayList

      public CopyOnWriteArrayList(Collection<? extends E> c)
    • CopyOnWriteArrayList

      public CopyOnWriteArrayList(E[] toCopyIn)
  • Method Details

    • size

      public int size()
      Description copied from interface: List

      Returns the number of elements in this List.

      Returns

      the number of elements in this List.

      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Returns:
      how many objects this Collection contains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in this Collection.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: List

      Returns whether this List contains no elements.

      Returns
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface List<E>
      Returns:

      true if this List has no elements, false otherwise.

      See also
      • #size
    • contains

      public boolean contains(Object o)
      Description copied from interface: List

      Tests whether this List contains the specified object.

      Parameters
      • object: the object to search for.
      Returns
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Returns:
      true if object is an element of this List, false otherwise
    • indexOf

      public int indexOf(Object o)
      Description copied from interface: List

      Searches this List for the specified object and returns the index of the first occurrence.

      Parameters
      • object: the object to search for.
      Returns
      Specified by:
      indexOf in interface List<E>
      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)
      Description copied from interface: List

      Searches this List for the specified object and returns the index of the last occurrence.

      Parameters
      • object: the object to search for.
      Returns
      Specified by:
      lastIndexOf in interface List<E>
      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.
      Overrides:
      clone in class Object
    • toArray

      public Object[] toArray()
      Description copied from interface: List

      Returns an array containing all elements contained in this List.

      Returns

      an array of the elements from this List.

      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Description copied from interface: List

      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
      • array: the array.
      Returns

      an array of the elements from this List.

      Throws
      • ArrayStoreException: @throws ArrayStoreException if the type of an element in this List cannot be stored in the type of the specified array.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface List<E>
    • get

      public E get(int index)
      Description copied from interface: List

      Returns the element at the specified location in this List.

      Parameters
      • location: the index of the element to return.
      Returns

      the element at the specified location.

      Throws
      • IndexOutOfBoundsException: if location = size()
      Specified by:
      get in interface List<E>
    • set

      public E set(int index, E element)
      Description copied from interface: List

      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
      • location: the index at which to put the specified object.

      • object: the object to insert.

      Returns

      the previous element at the index.

      Throws
      • UnsupportedOperationException: if replacing elements in this List is not supported.

      • ClassCastException: @throws 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()

      Specified by:
      set in interface List<E>
    • add

      public boolean add(E e)
      Description copied from interface: List

      Adds the specified object at the end of this List.

      Parameters
      • object: the object to add.
      Returns

      always true.

      Throws
      • UnsupportedOperationException: if adding to this List is not supported.

      • ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this List.

      • IllegalArgumentException: if the object cannot be added to this List.

      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if adding to this Collection is not supported.

      • ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection.

      • IllegalArgumentException: if the object cannot be added to this Collection.

      • NullPointerException: if null elements cannot be added to the Collection.

    • add

      public void add(int index, E element)
      Description copied from interface: List

      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
      • location: the index at which to insert.

      • object: the object to add.

      Throws
      • UnsupportedOperationException: if adding to this List is not supported.

      • ClassCastException: @throws 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()

      Specified by:
      add in interface List<E>
    • remove

      public E remove(int index)
      Description copied from interface: List

      Removes the object at the specified location from this List.

      Parameters
      • location: 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()

      Specified by:
      remove in interface List<E>
    • remove

      public boolean remove(Object o)
      Description copied from interface: List

      Removes the first occurrence of the specified object from this List.

      Parameters
      • object: the object to remove.
      Returns
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      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)
      Description copied from interface: List

      Tests whether this List contains all objects contained in the specified collection.

      Parameters
      • collection: the collection of objects
      Returns
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface List<E>
      Returns:
      true if all objects in the specified collection are elements of this List, false otherwise.
    • removeAll

      public boolean removeAll(Collection<?> c)
      Description copied from interface: List

      Removes all occurrences in this List of each object in the specified collection.

      Parameters
      • 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.
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface List<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if removing from this Collection is not supported.

      • ClassCastException: @throws ClassCastException if one or more elements of collection isn't of the correct type.

      • NullPointerException: @throws NullPointerException if collection contains at least one null element and this Collection doesn't support null elements.

      • NullPointerException: if collection is null.

    • retainAll

      public boolean retainAll(Collection<?> c)
      Description copied from interface: List

      Removes all objects from this List that are not contained in the specified collection.

      Parameters
      • 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.
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface List<E>
      Returns:

      true if this Collection is modified, false otherwise.

      Throws
      • UnsupportedOperationException: if removing from this Collection is not supported.

      • ClassCastException: @throws ClassCastException if one or more elements of collection isn't of the correct type.

      • NullPointerException: @throws NullPointerException if collection contains at least one null element and this Collection doesn't support null elements.

      • NullPointerException: if collection is null.

    • addAllAbsent

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

      public void clear()
      Description copied from interface: List

      Removes all elements from this List, leaving it empty.

      Throws
      • UnsupportedOperationException: if removing from this List is not supported.
      See also
      • #isEmpty

      • #size

      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface List<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Description copied from interface: List

      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
      • collection: the collection of objects.
      Returns
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      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: @throws 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)
      Description copied from interface: List

      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
      • location: the index at which to insert.

      • collection: the collection of objects to be inserted.

      Returns
      Specified by:
      addAll in interface List<E>
      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: @throws 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)
      Description copied from interface: Iterable
      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.
      Specified by:
      forEach in interface Iterable<E>
    • removeIf

      public boolean removeIf(Predicate<? super E> filter)
      Description copied from interface: Collection
      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.
      Specified by:
      removeIf in interface Collection<E>
    • replaceAll

      public void replaceAll(UnaryOperator<E> operator)
      Description copied from interface: List
      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.
      Specified by:
      replaceAll in interface List<E>
    • sort

      public void sort(Comparator<? super E> c)
      Description copied from interface: List
      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.
      Specified by:
      sort in interface List<E>
    • toString

      public String toString()
      Description copied from class: Object
      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())
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Description copied from interface: List

      Compares the given object with the List, and returns true if they represent the same object using a class specific comparison. For Lists, this means that they contain the same elements in exactly the same order.

      Parameters
      • object: the object to compare with this object.
      Returns
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface List<E>
      Overrides:
      equals in class Object
      Returns:

      boolean true if the object is the same as this object, and false if it is different from this object.

      See also
      • #hashCode
    • hashCode

      public int hashCode()
      Description copied from interface: List

      Returns the hash code for this List. It is calculated by taking each element' hashcode and its position in the List into account.

      Returns

      the hash code of the List.

      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface List<E>
      Overrides:
      hashCode in class Object
    • iterator

      public Iterator<E> iterator()
      Description copied from interface: List

      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
      • Iterator
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface List<E>
    • listIterator

      public ListIterator<E> listIterator()
      Description copied from interface: List

      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
      Specified by:
      listIterator in interface List<E>
    • listIterator

      public ListIterator<E> listIterator(int index)
      Description copied from interface: List

      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
      • location: 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
      • ListIterator
      Specified by:
      listIterator in interface List<E>
    • 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.
      Specified by:
      subList in interface List<E>