I am trying to understand the features of Spliterator and came across these 2 methods estimatedSize
and getExactSizeIfKnown
I could figure out what is estimatedSize
but not sure exactly what doesgetExactSizeIfKnown
do. Can someone please give an example explaining the difference between the two.
EDIT: I tried the following example in which both of them are the same. In which cases would they be different?
public static void main(String[] args) {
List<Integer> l = new ArrayList<>();
l.add(1);
l.add(2);
l.add(3);
Spliterator<Integer> s= (Spliterator<Integer>) l.spliterator();
Spliterator<Integer> s1=s.trySplit();
while(s.tryAdvance(n -> {System.out.print(n+" ");System.out.println("estimateSize "+s.estimateSize()+" getexactsizeifknown "+s.getExactSizeIfKnown());}));
Differences Between Iterator And Spliterator In Java 8 :Iterator performs only iteration over a set of elements. But, Spliterator splits as well as iterates over a set of elements which is very useful in parallel processing of elements.
Like Iterator and ListIterator, Spliterator is a Java Iterator, which is used to iterate elements one-by-one from a List implemented object. Some important points about Java Spliterator are: Java Spliterator is an interface in Java Collection API.
The estimateSize
method:
Returns an estimate of the number of elements that would be encountered by a
forEachRemaining(java.util.function.Consumer<? super T>)
traversal, or returnsLong.MAX_VALUE
if infinite, unknown, or too expensive to compute.If this Spliterator is
SIZED
and has not yet been partially traversed or split, or this Spliterator isSUBSIZED
and has not yet been partially traversed, this estimate must be an accurate count of elements that would be encountered by a complete traversal. Otherwise, this estimate may be arbitrarily inaccurate, but must decrease as specified across invocations oftrySplit()
.API Note:
Even an inexact estimate is often useful and inexpensive to compute. For example, a sub-spliterator of an approximately balanced binary tree may return a value that estimates the number of elements to be half of that of its parent; if the root Spliterator does not maintain an accurate count, it could estimate size to be the power of two corresponding to its maximum depth.
And the getExactSizeIfKnown
method is a:
Convenience method that returns
estimateSize()
if this Spliterator isSIZED
, else-1
.Implementation Requirements:
The default implementation returns the result of
estimateSize()
if the Spliterator reports a characteristic ofSIZED
, and-1
otherwise.
Both of those methods reference SIZED
, which is a:
Characteristic value signifying that the value returned from
estimateSize()
prior to traversal or splitting represents a finite size that, in the absence of structural source modification, represents an exact count of the number of elements that would be encountered by a complete traversal.API Note:
Most Spliterators for Collections, that cover all elements of a
Collection
report this characteristic. Sub-spliterators, such as those forHashSet
, that cover a sub-set of elements and approximate their reported size do not.
Based on all of this, the two methods will only ever return different values if the Spliterator
does not have the SIZED
characteristic.
In your example, the source of the Spliterator
is an ArrayList
. If we take a look at the documentation of ArrayList.spliterator()
:
Creates a late-binding and fail-fast
Spliterator
over the elements in this list.The
Spliterator
reportsSpliterator.SIZED
,Spliterator.SUBSIZED
, andSpliterator.ORDERED
. Overriding implementations should document the reporting of additional characteristic values.
Due to the SUBSIZED
characteristic, a Spliterator
created from an ArrayList
—including those resulting from trySplit
—will never have estimateSize
and getExactSizeIfKnown
return different values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With