为什么 HashSet return -1 for getExactSizeIfKnown
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
return -1
用于 HashSet
?
对于其他集合,它 return 与 estimateSIze()
的输出相同
The Javadoc 说明原因:
Convenience method that returns estimateSize() if this Spliterator is SIZED, else -1.
所以,您的拆分器尺寸不合适。
我试图了解 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
return -1
用于 HashSet
?
对于其他集合,它 return 与 estimateSIze()
The Javadoc 说明原因:
Convenience method that returns estimateSize() if this Spliterator is SIZED, else -1.
所以,您的拆分器尺寸不合适。