Spliterator - sized vs subsized flags

Stav Alfi :

https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html

SIZED 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.

SUBSIZED Characteristic value signifying that all Spliterators resulting from trySplit() will be both SIZED and SUBSIZED.

  1. Is there a situation when the SIZED flag is on but the SUBSIZED flag is off?
  2. Is there a situation when the SUBSIZED flag is on but the SIZED flag is off?
Holger :

A typical example of a Spliterator that is SIZED but not SUBSIZED, is the Spliterator created from a HashMap. It will maintain a range over its internal array of entries, some of these array entries being null, as the capacity is higher than the actual size. The exact distribution of the null entries to skip, depends on the hash codes of the contained keys.

So the Spliterator does know its (total) size initially, but when splitting the range, it doesn’t know how many elements are in each range. The more elements the HashMap has, the higher the likelihood of a roughly balanced split, so this strategy is reasonable, but the exact subsizes are not known and it would require an iteration over the array to find out.

Reporting a SUBSIZED characteristic without SIZED makes no sense and is, as far as I understood, not even valid.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=465820&siteId=1