Class CopyOnWriteArraySet<E>
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>
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 -
Method Summary
Modifier and TypeMethodDescriptionbooleanAttempts to addobjectto the contents of thisCollection(optional).booleanaddAll(Collection<? extends E> c) Attempts to add all of the objects contained incollectionto the contents of thisCollection(optional).voidclear()Removes all elements from thisCollection, leaving it empty (optional).booleanTests whether thisCollectioncontains the specified object.booleancontainsAll(Collection<?> c) Tests whether thisCollectioncontains all objects contained in the specifiedCollection.voidPerforms the given action for each element of theIterable.booleanisEmpty()Returns if thisCollectioncontains no elements.iterator()The elements as they were when this was called, in the order they were added.booleanRemoves one instance of the specified object from thisCollectionif one is contained (optional).booleanremoveAll(Collection<?> c) Removes all occurrences in this collection which are contained in the specified collection.booleanRemoves all of the elements of this collection that satisfy the given predicate.booleanretainAll(Collection<?> c) Removes all objects from thisCollectionthat are not also found in theCollectionpassed (optional).intsize()Returns a count of how many objects thisCollectioncontains.Object[]toArray()Returns a new array containing all elements contained in thisArrayList.<T> T[]toArray(T[] a) Returns an array containing all elements contained in thisArrayList.Methods inherited from class AbstractSet
equals, hashCodeMethods inherited from class AbstractCollection
toStringMethods inherited from interface Collection
parallelStream, stream, toArrayMethods inherited from interface Set
spliterator
-
Constructor Details
-
CopyOnWriteArraySet
public CopyOnWriteArraySet() -
CopyOnWriteArraySet
-
-
Method Details
-
size
public int size()Description copied from class:AbstractCollectionReturns a count of how many objects this
Collectioncontains.In this class this method is declared abstract and has to be implemented by concrete
Collectionimplementations.Returns
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractCollectionReturns if this
Collectioncontains no elements. This implementation tests, whethersizereturns 0.Returns
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.See also
- #size
-
contains
Description copied from class:AbstractCollectionTests whether this
Collectioncontains the specified object. This implementation iterates over thisCollectionand tests, whether any element is equal to the given object. Ifobject != nullthenobject.equals(e)is called for each elementereturned by the iterator until the element is found. Ifobject == nullthen each elementereturned by the iterator is compared with the teste == null.Parameters
object: the object to search for.
Returns
trueif object is an element of thisCollection,falseotherwise.Throws
-
ClassCastException: if the object to look for isn't of the correct type. -
NullPointerException: @throws NullPointerException if the object to look for isnulland thisCollectiondoesn't supportnullelements.
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueif object is an element of thisCollection,falseotherwise.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 isnulland thisCollectiondoesn't supportnullelements.
-
-
toArray
Description copied from class:AbstractCollectionReturns a new array containing all elements contained in this
ArrayList.Returns
an array of the elements from this
ArrayList- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a) Description copied from class:AbstractCollectionReturns 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 thisArrayList, 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 thisArrayListcannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
clear
public void clear()Description copied from class:AbstractCollectionRemoves all elements from this
Collection, leaving it empty (optional). This implementation iterates over thisCollectionand calls theremovemethod on each element. If the iterator does not support removal of elements, anUnsupportedOperationExceptionis thrown.Concrete implementations usually can clear a
Collectionmore efficiently and should therefore overwrite this method.Throws
UnsupportedOperationException: @throws UnsupportedOperationException it the iterator does not support removing elements from thisCollection
See also
-
#iterator
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from this
Collectionif one is contained (optional). This implementation iterates over thisCollectionand tests for each elementereturned by the iterator, whethereis equal to the given object. Ifobject != nullthen this test is performed usingobject.equals(e), otherwise usingobject == null. If an element equal to the given object is found, then theremovemethod is called on the iterator andtrueis returned,falseotherwise. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: if the object passed is not of the correct type. -
NullPointerException: @throws NullPointerException ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
-
add
Description copied from interface:CollectionAttempts to add
objectto the contents of thisCollection(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,falseif no changes were made.An implementation of
Collectionmay 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 anIllegalArgumentExceptionis 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:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<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.
-
-
containsAll
Description copied from class:AbstractCollectionTests whether this
Collectioncontains all objects contained in the specifiedCollection. This implementation iterates over the specifiedCollection. If one element returned by the iterator is not contained in thisCollection, thenfalseis returned;trueotherwise.Parameters
collection: the collection of objects.
Returns
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>- Overrides:
containsAllin classAbstractCollection<E>- Returns:
trueif all objects in the specifiedCollectionare elements of thisCollection,falseotherwise.Throws
-
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.
-
-
addAll
Description copied from class:AbstractCollectionAttempts to add all of the objects contained in
collectionto the contents of thisCollection(optional). This implementation iterates over the givenCollectionand callsaddfor each element. If any of these calls returntrue, thentrueis returned as result of this method call,falseotherwise. If thisCollectiondoes not support adding elements, anUnsupportedOperationExceptionis thrown.If the passed
Collectionis changed during the process of adding elements to thisCollection, the behavior depends on the behavior of the passedCollection.Parameters
collection: the collection of objects.
Returns
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisCollection. -
IllegalArgumentException: if an object cannot be added to thisCollection. -
NullPointerException: @throws NullPointerException ifcollectionisnull, or if it containsnullelements and thisCollectiondoes not support such elements.
-
-
removeAll
Description copied from class:AbstractSetRemoves all occurrences in this collection which are contained in the specified collection.
Parameters
collection: the collection of objects to remove.
Returns
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>- Returns:
trueif this collection was modified,falseotherwise.Throws
UnsupportedOperationException: if removing from this collection is not supported.
-
retainAll
Description copied from class:AbstractCollectionRemoves all objects from this
Collectionthat are not also found in theCollectionpassed (optional). After this method returns thisCollectionwill only contain elements that also can be found in theCollectionpassed to this method.This implementation iterates over this
Collectionand tests for each elementereturned by the iterator, whether it is contained in the specifiedCollection. If this test is negative, then theremovemethod is called on the iterator. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
collection: the collection of objects to retain.
Returns
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<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.
-
-
iterator
The elements as they were when this was called, in the order they were added.removeon the iterator throwsUnsupportedOperationException. -
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>
-
forEach
-