gho*_*der 5 java java-8 spliterator
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 doesgetExactSizeIfKnowndo. 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());}));
Run Code Online (Sandbox Code Playgroud)
该estimateSize方法:
返回
forEachRemaining(java.util.function.Consumer<? super T>)遍历将遇到的元素数量的估计值,或者返回Long.MAX_VALUE无穷大,未知或过于昂贵而无法计算的元素。如果此分割器已经
SIZED并且尚未被部分遍历或拆分,或者此分割器已经SUBSIZED并且尚未被部分遍历,则此估计值必须是完整遍历会遇到的元素的准确计数。否则,此估算值可能是任意不正确的,但必须按照调用之间的指定减少trySplit()。API注意:
即使是不精确的估算值,也通常对计算有用且便宜。例如,近似平衡的二叉树的子拆分器可能返回一个值,该值估计元素的数量为其父代的一半。如果根分离器不能保持准确的计数,则可以将大小估计为与其最大深度相对应的2的幂。
和getExactSizeIfKnown方法是:
estimateSize()如果此Spliterator为SIZEDelse ,则返回的便捷方法-1。实施要求:
默认实现返回
estimateSize()如果Spliterator报告特性为的结果SIZED,-1否则返回。
这两个方法都参考SIZED,这是:
表示
estimateSize()遍历或分割之前返回的值的特征值表示有限大小,在没有结构源修改的情况下,该大小表示完整遍历将遇到的元素数的精确计数。API注意:
集合的大多数拆分器,涵盖了
Collection此特征的报告的所有元素。子分类器(例如用于HashSet的子分类器)不包含元素的子集并近似于其报告的大小。
基于所有这些,这两种方法只会在Spliterator不具有SIZED 特征的情况下返回不同的值。
在您的示例中,的来源Spliterator是ArrayList。如果我们看一下以下文档ArrayList.spliterator():
在此列表中的元素上创建后期绑定和故障
Spliterator转移。该
Spliterator报告Spliterator.SIZED,Spliterator.SUBSIZED和Spliterator.ORDERED。首要实现应记录其他特征值的报告。
由于该SUBSIZED特性,Spliterator由ArrayList-所创建的-包括由-所产生的那些trySplit-将永远不会具有estimateSize并getExactSizeIfKnown返回不同的值。
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |