Separator with dimensions against sub-flagged flags

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

SIZED A characteristic value that means that the value returned from the Size () estimate before crawling or splitting is the final size, which, in the absence of structural modification of the source, is an accurate count of the number of elements that will encounter a full crawl.

SUBSIZED A characteristic value that means that all delimiters resulting from trySplit () will be used by both SIZED and SUBSIZED.

  • Is there a situation when the SIZED flag is on but the SUBSIZED flag is off?
  • Is there a situation when the SUBSIZED flag is on but the SIZED flag is off?
+5
source share
2 answers

A typical example of a Spliterator SIZED , but not SUBSIZED , is a Spliterator created using the HashMap . It will maintain the range of the internal array of records, and some of these records in the array are null , since the capacity is higher than the actual one. The exact distribution of null entries for omissions depends on the hash codes of the keys contained.

So, Spliterator first knows its (total) size, but when splitting a range, it does not know how many elements in each range. The more elements the HashMap has, the higher the likelihood of a balanced split, so this strategy is reasonable, but the exact subseries are unknown, and this will require iteration over the array.

The SUBSIZED feature SUBSIZED without SIZED does not make sense and, as I understand it, does not matter.

+7
source

A little late, but still ... It also puzzled me. I will list those that I know of:

HashMap (as noted above) and as such IdentityHashMap , LinkedHashMap and TreeMap .

It should be noted here that ConcurrentHashMap not listed because it allows simultaneous updates, so its size is actually equal to the size at the time of the call. In fact, CHM does not explicitly report SIZED .

Then there are those related to Map , for example HashSet and TreeSet , because inside they are still Maps.

And then there is one that is not expected a bit here, in the face of BitSet , for which BitSetSpliterator#characteristics looks like:

  @Override public int characteristics() { // Only sized when root and not split return (root ? Spliterator.SIZED : 0) | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED; } 

This may sound funny, but the explanation is as follows:

 // Raise the index of this spliterator to be the next set bit // from the mid point index = nextSetBit(mid, wordIndex(hi - 1)); 

So, for BitSet this will happen only once, and there is no point in reporting SUBSIZED , because the split will not occur exactly in the middle.

Quite the opposite: do not report SUBSIZED , but not SIZED , so no one (as I understand it in the code) does this.

+2
source

Source: https://habr.com/ru/post/1268481/


All Articles