Class ListOfUnknownSize<E>
Object
AbstractCollection<E>
AbstractList<E>
AbstractSequentialList<E>
ListOfUnknownSize<E>
- Type Parameters:
E- the type of elements in the list.
- All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>
An alternative to
AbstractList 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
Despite extendingAbstractSequentialList, subclasses are not expected to overwrite the
listIterator(int) method. Instead, subclasses should implement the following methods:
get(int)isValidIndex(int)sizeIfKnown()— optional but recommended for efficiencycharacteristics()— if the implementation disallows null elements.
- Since:
- 1.6
-
Field Summary
Fields inherited from class AbstractList
modCount -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new list of initially unknown size. -
Method Summary
Modifier and TypeMethodDescriptionprotected intReturns characteristics of this collection as a combination ofSpliteratorbits.abstract Eget(int index) Returns the element at the specified index.booleanisEmpty()Returnstrueif this list is empty.protected abstract booleanisValidIndex(int index) Returnstrueif the given index is valid for this list.listIterator(int index) Returns a list iterator over the elements in this list.booleanremoveAll(Collection<?> c) Removes elements of the given collection from this list.intsize()Returns the number of elements in this list.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 AbstractList
add, clear, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subListMethods inherited from class AbstractCollection
addAll, contains, containsAll, 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 List
addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, remove, removeFirst, removeLast, replaceAll, retainAll, reversed, sort
-
Constructor Details
-
ListOfUnknownSize
protected ListOfUnknownSize()Creates a new list of initially unknown size.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Returnstrueif this list is empty. The implementation of this method avoids to invokesize().- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif this list is empty.
-
size
public int size()Returns the number of elements in this list. The default implementation returns the size if known, or otherwise searches the smallest value for whichisValidIndex(int)returnsfalse.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this list.
-
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, someListOfUnknownSizemethods will take a more efficient path.- Returns:
size()if its value is already known, or empty if this value is still costly to compute.
-
isValidIndex
protected abstract boolean isValidIndex(int index) Returnstrueif the given index is valid for this list. If the index i is valid, then all indices from 0 to i - 1 inclusive are presumed also valid. Invoking this method is usually less expansive than testingindex < size()when the size is costly to compute.- Parameters:
index- the index where to verify if an element exists.- Returns:
trueif an element exists at the given index.
-
get
Returns the element at the specified index. Invoking this method may trig computation of the element if these computations are deferred.- Specified by:
getin interfaceList<E>- Overrides:
getin classAbstractSequentialList<E>- Parameters:
index- position of the element to get in this list.- Returns:
- the element at the given index.
- Throws:
IndexOutOfBoundsException- if the given index is out of bounds.
-
removeAll
Removes elements of the given collection from this list. The implementation of this method avoids to invokesize().- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Parameters:
c- the collection containing elements to remove.- Returns:
trueif at least one element has been removed.
-
listIterator
Returns a list iterator over the elements in this list. The default implementation delegates toisValidIndex(int)andget(int). Write operations are not supported.- Specified by:
listIteratorin interfaceList<E>- Specified by:
listIteratorin classAbstractSequentialList<E>- Parameters:
index- index of first element to be returned from the list.- Returns:
- a list iterator over the elements in this list.
- Throws:
IndexOutOfBoundsException- if the given index is out of bounds.
-
characteristics
protected int characteristics()Returns characteristics of this collection as a combination ofSpliteratorbits. The default implementation returnsSpliterator.ORDERED. Subclasses should add theSpliterator.NONNULLbit if they guarantee that this list does not contain anynullelement.- 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 interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array containing all list 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 interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Type Parameters:
T- the type array elements.- Parameters:
array- where to store the elements.- Returns:
- an array containing all list 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 list elements.
-