繁体   English   中英

为什么HashSet对于getExactSizeIfKnown返回-1

[英]Why does HashSet return -1 for getExactSizeIfKnown

我试图了解SplitIterator的功能,并遇到以下问题:

public static void main(String[] args) {
    Set<Integer> l = new HashSet();
    l.add(1);
    l.add(2);
    l.add(3);
    Spliterator<Integer> s= (Spliterator<Integer>) l.spliterator();
Spliterator<Integer> s1=s.trySplit();
while(s1.tryAdvance(n -> {System.out.print(n+" ");System.out.println("estimateSize "+s.estimateSize()+" getexactsizeifknown "+s.getExactSizeIfKnown());})); 

为什么getexactsizeifknown方法为HashSet返回-1

对于其他集合,它返回与estimateSIze()相同的输出。

Javadoc描述了原因:

如果此分隔符为SIZED,则返回EstimateSize()的便捷方法,否则为-1。

因此,您的分离器不是SIZED。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM