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>
- Object
- 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()List.Returns
List.isEmpty
public boolean isEmpty()List contains no elements.Returns
true if this List has no elements, false
otherwise.See also
contains
public boolean contains(Object o)List contains the specified object.Parameters
oObject- the object to search for.
Returns
true if object is an element of this List, false
otherwiseindexOf
public int indexOf(Object o)List for the specified object and returns the index of the
first occurrence.Parameters
oObject- the object to search for.
Returns
indexOf
public int indexOf(E e, int index)lastIndexOf
public int lastIndexOf(Object o)List for the specified object and returns the index of the
last occurrence.Parameters
oObject- the object to search for.
Returns
lastIndexOf
public int lastIndexOf(E e, int index)clone
public Object clone()toArray
public Object[] toArray()List.Returns
List.toArray
public <T> T[] toArray(T[] a)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
aT[]- the array.
Returns
List.Throws
ArrayStoreException- if the type of an element in this
Listcannot be stored in the type of the specified array.
get
public E get(int index)List.Parameters
indexint- the index of the element to return.
Returns
Throws
IndexOutOfBoundsException- if
location = size()
set
public E set(int index, E element)List with the
specified object. This operation does not change the size of the List.Parameters
indexint- the index at which to put the specified object.
elementE- the object to insert.
Returns
Throws
UnsupportedOperationException- if replacing elements in this
Listis 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)List.Parameters
eE- the object to add.
Returns
Throws
UnsupportedOperationException- if adding to this
Listis 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)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
indexint- the index at which to insert.
elementE- the object to add.
Throws
UnsupportedOperationException- if adding to this
Listis 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)List.Parameters
indexint- the index of the object to remove.
Returns
Throws
UnsupportedOperationException- if removing from this
Listis not supported. IndexOutOfBoundsException- if
location = size()
remove
public boolean remove(Object o)List.Parameters
oObject- the object to remove.
Returns
List was modified by this operation, false
otherwise.Throws
UnsupportedOperationException- if removing from this
Listis not supported.
addIfAbsent
public boolean addIfAbsent(E e)containsAll
public boolean containsAll(Collection<?> c)List contains all objects contained in the
specified collection.Parameters
cCollection<?>- 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)List of each object in the specified
collection.Parameters
cCollection<?>- the collection of objects to remove.
Returns
true if this List is modified, false otherwise.Throws
UnsupportedOperationException- if removing from this
Listis not supported.
retainAll
public boolean retainAll(Collection<?> c)List that are not contained in the
specified collection.Parameters
cCollection<?>- the collection of objects to retain.
Returns
true if this List is modified, false otherwise.Throws
UnsupportedOperationException- if removing from this
Listis not supported.
addAllAbsent
public int addAllAbsent(Collection<? extends E> c)clear
public void clear()List, leaving it empty.Throws
UnsupportedOperationException- if removing from this
Listis not supported.
addAll
public boolean addAll(Collection<? extends E> c)List. The
objects are added in the order in which they are returned from the
collection’s iterator.Parameters
cCollection<? 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
Listis 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)List. The objects are added in the order they are returned from
the collection’s iterator.Parameters
indexint- the index at which to insert.
cCollection<? extends E>- the collection of objects to be inserted.
Returns
List has been modified through the insertion, false
otherwise (i.e. if the passed collection was empty).Throws
UnsupportedOperationException- if adding to this
Listis 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)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)replaceAll
public void replaceAll(UnaryOperator<E> operator)sort
public void sort(Comparator<? super E> c)Comparator. Stubbed in the CLDC11 subset; the actual
implementation comes from the platform’s JDK at runtime.toString
public String toString()equals
public boolean equals(Object o)hashCode
public int hashCode()iterator
public Iterator<E> iterator()List. The elements are
iterated in the same order as they occur in the List.Returns
List.See also
listIterator
public ListIterator<E> listIterator()List iterator on the elements of this List. The elements are
iterated in the same order that they occur in the List.Returns
List iterator on the elements of this ListSee also
listIterator
public ListIterator<E> listIterator(int index)List. The elements are
iterated in the same order as they occur in the List. The iteration
starts at the specified location.Parameters
indexint- the index at which to start the iteration.
Returns
List.Throws
IndexOutOfBoundsException- if
location size()
See also
subList
public List<E> subList(int fromIndex, int toIndex)UnsupportedOperationException here instead of a lost update.Parameters
fromIndexint- the index at which to start the sublist.
toIndexint- the index one past the end of the sublist.
Returns
List.Throws
IndexOutOfBoundsException- if
start endorend > size()