Class CopyOnWriteArrayList<E>
- All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
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
ConstructorsConstructorDescriptionCopyOnWriteArrayList(E[] toCopyIn) CopyOnWriteArrayList(Collection<? extends E> c) -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified object into thisListat the specified location.booleanAdds the specified object at the end of thisList.booleanaddAll(int index, Collection<? extends E> c) Inserts the objects in the specified collection at the specified location in thisList.booleanaddAll(Collection<? extends E> c) Adds the objects in the specified collection to the end of thisList.intaddAllAbsent(Collection<? extends E> c) booleanaddIfAbsent(E e) voidclear()Removes all elements from thisList, leaving it empty.clone()A list sharing this one's current array.booleanTests whether thisListcontains the specified object.booleancontainsAll(Collection<?> c) Tests whether thisListcontains all objects contained in the specified collection.booleanCompares the given object with theList, and returns true if they represent the same object using a class specific comparison.voidPerforms the given action for each element of theIterable.get(int index) Returns the element at the specified location in thisList.inthashCode()Returns the hash code for thisList.intintSearches thisListfor the specified object and returns the index of the first occurrence.booleanisEmpty()Returns whether thisListcontains no elements.iterator()Returns an iterator on the elements of thisList.intlastIndexOf(E e, int index) intSearches thisListfor the specified object and returns the index of the last occurrence.Returns aListiterator on the elements of thisList.listIterator(int index) Returns a list iterator on the elements of thisList.remove(int index) Removes the object at the specified location from thisList.booleanRemoves the first occurrence of the specified object from thisList.booleanremoveAll(Collection<?> c) Removes all occurrences in thisListof each object in the specified collection.booleanRemoves all of the elements of this collection that satisfy the given predicate.voidreplaceAll(UnaryOperator<E> operator) Replaces each element of this list with the result of applying the operator to that element.booleanretainAll(Collection<?> c) Removes all objects from thisListthat are not contained in the specified collection.Replaces the element at the specified location in thisListwith the specified object.intsize()Returns the number of elements in thisList.voidsort(Comparator<? super E> c) Sorts this list according to the order induced by the specifiedComparator.subList(int fromIndex, int toIndex) An unmodifiable copy of the range.Object[]toArray()Returns an array containing all elements contained in thisList.<T> T[]toArray(T[] a) Returns an array containing all elements contained in thisList.toString()Returns a string representation of the object.Methods inherited from interface Collection
parallelStream, stream, toArrayMethods inherited from interface List
addFirst, addLast, getFirst, getLast, removeFirst, removeLast, reversed, spliterator
-
Constructor Details
-
CopyOnWriteArrayList
public CopyOnWriteArrayList() -
CopyOnWriteArrayList
-
CopyOnWriteArrayList
-
-
Method Details
-
size
public int size()Description copied from interface:ListReturns the number of elements in this
List.Returns
the number of elements in this
List. -
isEmpty
-
contains
Description copied from interface:ListTests whether this
Listcontains the specified object.Parameters
object: the object to search for.
Returns
-
indexOf
Description copied from interface:ListSearches this
Listfor the specified object and returns the index of the first occurrence.Parameters
object: the object to search for.
Returns
-
indexOf
-
lastIndexOf
Description copied from interface:ListSearches this
Listfor the specified object and returns the index of the last occurrence.Parameters
object: the object to search for.
Returns
- Specified by:
lastIndexOfin interfaceList<E>- Returns:
- the index of the last occurrence of the object, or -1 if the object was not found.
-
lastIndexOf
-
clone
-
toArray
-
toArray
public <T> T[] toArray(T[] a) Description copied from interface:ListReturns 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 thisList, 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 thisListcannot be stored in the type of the specified array.
-
get
Description copied from interface:ListReturns 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: iflocation = size()
-
set
Description copied from interface:ListReplaces the element at the specified location in this
Listwith the specified object. This operation does not change the size of theList.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 thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation = size()
-
-
add
Description copied from interface:ListAdds the specified object at the end of this
List.Parameters
object: the object to add.
Returns
always true.
Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for thisList. -
IllegalArgumentException: if the object cannot be added to thisList.
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
add
Description copied from interface:ListInserts the specified object into this
Listat the specified location. The object is inserted before the current element at the specified location. If the location is equal to the size of thisList, the object is added at the end. If the location is smaller than the size of thisList, then all elements beyond the specified location are moved by one position towards the end of theList.Parameters
-
location: the index at which to insert. -
object: the object to add.
Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for thisList. -
IllegalArgumentException: if the object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation size()
-
-
remove
Description copied from interface:ListRemoves 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 thisListis not supported. -
IndexOutOfBoundsException: iflocation = size()
-
remove
Description copied from interface:ListRemoves the first occurrence of the specified object from this
List.Parameters
object: the object to remove.
Returns
-
addIfAbsent
-
containsAll
Description copied from interface:ListTests whether this
Listcontains all objects contained in the specified collection.Parameters
collection: the collection of objects
Returns
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>- Returns:
trueif all objects in the specified collection are elements of thisList,falseotherwise.
-
removeAll
Description copied from interface:ListRemoves all occurrences in this
Listof each object in the specified collection.Parameters
collection: the collection of objects to remove.
Returns
trueif thisListis modified,falseotherwise.Throws
UnsupportedOperationException: if removing from thisListis not supported.
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
retainAll
Description copied from interface:ListRemoves all objects from this
Listthat are not contained in the specified collection.Parameters
collection: the collection of objects to retain.
Returns
trueif thisListis modified,falseotherwise.Throws
UnsupportedOperationException: if removing from thisListis not supported.
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
addAllAbsent
-
clear
public void clear()Description copied from interface:ListRemoves all elements from this
List, leaving it empty.Throws
UnsupportedOperationException: if removing from thisListis not supported.
See also
-
#isEmpty
-
#size
-
addAll
Description copied from interface:ListAdds 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:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Returns:
trueif thisListis modified,falseotherwise (i.e. if the passed collection was empty).Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList.
-
-
addAll
Description copied from interface:ListInserts 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:
addAllin interfaceList<E>- Returns:
true if this
Listhas been modified through the insertion, false otherwise (i.e. if the passed collection was empty).Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation size()
-
-
-
forEach
-
removeIf
Description copied from interface:CollectionRemoves 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:
removeIfin interfaceCollection<E>
-
replaceAll
Description copied from interface:ListReplaces 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:
replaceAllin interfaceList<E>
-
sort
-
toString
Description copied from class:ObjectReturns 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
Description copied from interface:ListCompares the given object with the
List, and returns true if they represent the same object using a class specific comparison. ForLists, this means that they contain the same elements in exactly the same order.Parameters
object: the object to compare with this object.
Returns
-
hashCode
public int hashCode()Description copied from interface:ListReturns the hash code for this
List. It is calculated by taking each element' hashcode and its position in theListinto account.Returns
the hash code of the
List. -
iterator
Description copied from interface:ListReturns an iterator on the elements of this
List. The elements are iterated in the same order as they occur in theList.Returns
an iterator on the elements of this
List.See also
- Iterator
-
listIterator
Description copied from interface:ListReturns a
Listiterator on the elements of thisList. The elements are iterated in the same order that they occur in theList.Returns
a
Listiterator on the elements of thisListSee also
- ListIterator
- Specified by:
listIteratorin interfaceList<E>
-
listIterator
Description copied from interface:ListReturns a list iterator on the elements of this
List. The elements are iterated in the same order as they occur in theList. 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: iflocation size()
See also
- ListIterator
- Specified by:
listIteratorin interfaceList<E>
-
subList
-