Class CopyOnWriteArraySet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.concurrent.CopyOnWriteArraySet<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>

public class CopyOnWriteArraySet<E> extends AbstractSet<E> implements Serializable

java.util.concurrent.CopyOnWriteArraySet for the Codename One runtime: a set kept in a CopyOnWriteArrayList, in the order its elements were added.

Every mutation replaces the backing array with a modified copy, and an iterator keeps the array that was current when it was created, so a listener that removes itself while the set is being walked neither disturbs the walk nor throws ConcurrentModificationException. Iterators are read only, as in the JDK.

It is as safe to use from several threads at once as the list it is kept in.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(E e)
    Attempts to add object to the contents of this Collection (optional).
    boolean
    addAll(Collection<? extends E> c)
    Attempts to add all of the objects contained in collection to the contents of this Collection (optional).
    void
    Removes all elements from this Collection, leaving it empty (optional).
    boolean
    Tests whether this Collection contains the specified object.
    boolean
    Tests whether this Collection contains all objects contained in the specified Collection.
    void
    forEach(Consumer<? super E> action)
    Performs the given action for each element of the Iterable.
    boolean
    Returns if this Collection contains no elements.
    The elements as they were when this was called, in the order they were added.
    boolean
    Removes one instance of the specified object from this Collection if one is contained (optional).
    boolean
    Removes all occurrences in this collection which are contained in the specified collection.
    boolean
    removeIf(Predicate<? super E> filter)
    Removes all of the elements of this collection that satisfy the given predicate.
    boolean
    Removes all objects from this Collection that are not also found in the Collection passed (optional).
    int
    Returns a count of how many objects this Collection contains.
    Returns a new array containing all elements contained in this ArrayList.
    <T> T[]
    toArray(T[] a)
    Returns an array containing all elements contained in this ArrayList.

    Methods inherited from class AbstractSet

    equals, hashCode

    Methods inherited from class AbstractCollection

    toString

    Methods inherited from class Object

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

    Methods inherited from interface Collection

    parallelStream, stream, toArray

    Methods inherited from interface Set

    spliterator
  • Constructor Details

    • CopyOnWriteArraySet

      public CopyOnWriteArraySet()
    • CopyOnWriteArraySet

      public CopyOnWriteArraySet(Collection<? extends E> c)
  • Method Details

    • size

      public int size()
      Description copied from class: AbstractCollection

      Returns a count of how many objects this Collection contains.

      In this class this method is declared abstract and has to be implemented by concrete Collection implementations.

      Returns
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<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 class: AbstractCollection

      Returns if this Collection contains no elements. This implementation tests, whether size returns 0.

      Returns
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:

      true if this Collection has no elements, false otherwise.

      See also
      • #size
    • contains

      public boolean contains(Object o)
      Description copied from class: AbstractCollection

      Tests whether this Collection contains the specified object. This implementation iterates over this Collection and tests, whether any element is equal to the given object. If object != null then object.equals(e) is called for each element e returned by the iterator until the element is found. If object == null then each element e returned by the iterator is compared with the test e == null.

      Parameters
      • object: the object to search for.
      Returns

      true if object is an element of this Collection, false otherwise.

      Throws
      • ClassCastException: if the object to look for isn't of the correct type.

      • NullPointerException: @throws NullPointerException if the object to look for is null and this Collection doesn't support null elements.

      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
      Returns:

      true if object is an element of this Collection, false otherwise.

      Throws
      • ClassCastException: @throws ClassCastException if the object to look for isn't of the correct type.

      • NullPointerException: @throws NullPointerException if the object to look for is null and this Collection doesn't support null elements.

    • toArray

      public Object[] toArray()
      Description copied from class: AbstractCollection

      Returns a new array containing all elements contained in this ArrayList.

      Returns

      an array of the elements from this ArrayList

      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • toArray

      public <T> T[] toArray(T[] a)
      Description copied from class: AbstractCollection

      Returns an array containing all elements contained in this ArrayList. 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 ArrayList, the array element following the collection elements is set to null.

      Parameters
      • contents: the array.
      Returns

      an array of the elements from this ArrayList.

      Throws
      • ArrayStoreException: @throws ArrayStoreException when the type of an element in this ArrayList cannot be stored in the type of the specified array.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • clear

      public void clear()
      Description copied from class: AbstractCollection

      Removes all elements from this Collection, leaving it empty (optional). This implementation iterates over this Collection and calls the remove method on each element. If the iterator does not support removal of elements, an UnsupportedOperationException is thrown.

      Concrete implementations usually can clear a Collection more efficiently and should therefore overwrite this method.

      Throws
      • UnsupportedOperationException: @throws UnsupportedOperationException it the iterator does not support removing elements from this Collection
      See also
      • #iterator

      • #isEmpty

      • #size

      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Description copied from class: AbstractCollection

      Removes one instance of the specified object from this Collection if one is contained (optional). This implementation iterates over this Collection and tests for each element e returned by the iterator, whether e is equal to the given object. If object != null then this test is performed using object.equals(e), otherwise using object == null. If an element equal to the given object is found, then the remove method is called on the iterator and true is returned, false otherwise. If the iterator does not support removing elements, an UnsupportedOperationException is thrown.

      Parameters
      • object: the object to remove.
      Returns
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
      Returns:

      true if this Collection is modified, false otherwise.

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

      • ClassCastException: if the object passed is not of the correct type.

      • NullPointerException: @throws NullPointerException if object is null and this Collection doesn't support null elements.

    • add

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

      Attempts to add object to the contents of this Collection (optional).

      After this method finishes successfully it is guaranteed that the object is contained in the collection.

      If the collection was modified it returns true, false if no changes were made.

      An implementation of Collection may narrow the set of accepted objects, but it has to specify this in the documentation. If the object to be added does not meet this restriction, then an IllegalArgumentException is thrown.

      If a collection does not yet contain an object that is to be added and adding the object fails, this method must throw an appropriate unchecked Exception. Returning false is not permitted in this case because it would violate the postcondition that the element will be part of the collection after this method finishes.

      Parameters
      • object: the object to add.
      Returns
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<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.

    • containsAll

      public boolean containsAll(Collection<?> c)
      Description copied from class: AbstractCollection

      Tests whether this Collection contains all objects contained in the specified Collection. This implementation iterates over the specified Collection. If one element returned by the iterator is not contained in this Collection, then false is returned; true otherwise.

      Parameters
      • collection: the collection of objects.
      Returns
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface Set<E>
      Overrides:
      containsAll in class AbstractCollection<E>
      Returns:

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

      Throws
      • 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.

    • addAll

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

      Attempts to add all of the objects contained in collection to the contents of this Collection (optional). This implementation iterates over the given Collection and calls add for each element. If any of these calls return true, then true is returned as result of this method call, false otherwise. If this Collection does not support adding elements, an UnsupportedOperationException is thrown.

      If the passed Collection is changed during the process of adding elements to this Collection, the behavior depends on the behavior of the passed Collection.

      Parameters
      • collection: the collection of objects.
      Returns
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
      Overrides:
      addAll in class AbstractCollection<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 an object is inappropriate for this Collection.

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

      • NullPointerException: @throws NullPointerException if collection is null, or if it contains null elements and this Collection does not support such elements.

    • removeAll

      public boolean removeAll(Collection<?> c)
      Description copied from class: AbstractSet

      Removes all occurrences in this collection which are contained in the specified collection.

      Parameters
      • collection: the collection of objects to remove.
      Returns
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
      Overrides:
      removeAll in class AbstractSet<E>
      Returns:

      true if this collection was modified, false otherwise.

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

      public boolean retainAll(Collection<?> c)
      Description copied from class: AbstractCollection

      Removes all objects from this Collection that are not also found in the Collection passed (optional). After this method returns this Collection will only contain elements that also can be found in the Collection passed to this method.

      This implementation iterates over this Collection and tests for each element e returned by the iterator, whether it is contained in the specified Collection. If this test is negative, then the remove method is called on the iterator. If the iterator does not support removing elements, an UnsupportedOperationException is thrown.

      Parameters
      • collection: the collection of objects to retain.
      Returns
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
      Overrides:
      retainAll in class AbstractCollection<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.

    • iterator

      public Iterator<E> iterator()
      The elements as they were when this was called, in the order they were added. remove on the iterator throws UnsupportedOperationException.
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<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>
    • 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>