Class SetOfUnknownSize<E>
Object
AbstractCollection<E>
AbstractSet<E>
SetOfUnknownSize<E>
- Type Parameters:
E- the type of elements in the set.
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
An alternative to
AbstractSet for implementations having a costly size() method.
The size of the set is not known in advance, but may become known after one complete iteration.
This class overrides methods in a way that reduces or eliminates the need to invoke size().
Note for implementers
Subclasses should implement the following methods:AbstractCollection.iterator()sizeIfKnown()— optional but recommended for efficiencycharacteristics()— if the implementation disallows null elements.
- Since:
- 1.6
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new set of initially unknown size. -
Method Summary
Modifier and TypeMethodDescriptionprotected intReturns characteristics of this collection as a combination ofSpliteratorbits.booleanReturnstrueif the given object is also a set and the two sets have the same content.booleanisEmpty()Returnstrueif this set is empty.booleanremoveAll(Collection<?> c) Removes elements of the given collection from this set.intsize()Returns the number of elements in this set.protected OptionalIntReturns thesize()value if it is already known, or empty if the size is still unknown.Creates aSpliteratorwhich may be of unknown size.Object[]toArray()Returns all elements in an array.<T> T[]toArray(IntFunction<T[]> generator) Returns the elements in an array generated by the given function.<T> T[]toArray(T[] array) Returns the elements in the given array, or in a new array if it was necessary to allocate more space.Methods inherited from class AbstractSet
hashCodeMethods inherited from class AbstractCollection
add, addAll, clear, contains, containsAll, iterator, remove, retainAll, toStringMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
parallelStream, removeIf, streamMethods inherited from interface Set
add, addAll, clear, contains, containsAll, iterator, remove, retainAll
-
Constructor Details
-
SetOfUnknownSize
protected SetOfUnknownSize()Creates a new set of initially unknown size.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrueif this set is empty. The implementation of this method avoids to invokesize().- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif this set is empty.
-
size
public int size()Returns the number of elements in this set. The default implementation returns the size if known, or otherwise counts the number of elements returned by the iterator.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this set.
-
sizeIfKnown
Returns thesize()value if it is already known, or empty if the size is still unknown. The default implementation always returns an empty value. Subclasses can overwrite this method if they have already computed and cached the number of elements. When this information is known, someSetOfUnknownSizemethods will take a more efficient path.- Returns:
size()if its value is already known, or empty if this value is still costly to compute.
-
removeAll
Removes elements of the given collection from this set. The implementation of this method avoids to invokesize().- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>- Parameters:
c- the collection containing elements to remove.- Returns:
trueif at least one element has been removed.
-
characteristics
protected int characteristics()Returns characteristics of this collection as a combination ofSpliteratorbits. The default implementation returnsSpliterator.DISTINCT. Subclasses should add theSpliterator.NONNULLbit if they guarantee that this set does not contain thenullelement.- Returns:
- the characteristics of this collection.
- See Also:
-
spliterator
Creates aSpliteratorwhich may be of unknown size.- Returns:
- a
Spliteratorover the elements in this collection.
-
toArray
Returns all elements in an array. The implementation of this method avoids to invokesize().- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array containing all set elements.
-
toArray
public <T> T[] toArray(T[] array) Returns the elements in the given array, or in a new array if it was necessary to allocate more space. If the given array is larger than necessary, the remaining array elements are set tonull. The implementation of this method avoids to invokesize().- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>- Type Parameters:
T- the type array elements.- Parameters:
array- where to store the elements.- Returns:
- an array containing all set elements.
-
toArray
Returns the elements in an array generated by the given function. The implementation of this method avoids to invokesize().- Type Parameters:
T- the type array elements.- Parameters:
generator- the function for allocating a new array.- Returns:
- an array containing all set elements.
-
equals
Returnstrueif the given object is also a set and the two sets have the same content. The implementation of this method avoids to invokesize()on this instance. However, it still callother.size().- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classAbstractSet<E>- Parameters:
other- the object to compare with this set.- Returns:
trueif the two set have the same content.
-