JAVA8学习——Stream底层的实现(学习过程) (7)

estimateSize() 估算大小。

/** * Returns an estimate of the number of elements that would be * encountered by a {@link #forEachRemaining} traversal, or returns {@link * Long#MAX_VALUE} if infinite, unknown, or too expensive to compute. 返回元素数量的估算值。(会被forEachRemaining 遍历的元素) infinite, unknown, or too expensive to compute.这几种情况会返回:MAX_VALUE * * <p>If this Spliterator is {@link #SIZED} and has not yet been partially * traversed or split, or this Spliterator is {@link #SUBSIZED} and has * not yet been partially traversed, this estimate must be an accurate * count of elements that would be encountered by a complete traversal. * Otherwise, this estimate may be arbitrarily inaccurate, but must decrease * as specified across invocations of {@link #trySplit}. 如果Spliterator是SIZED 或者是SUBSIZED 。那个 这个元素的estimate值一定是精确的。 然而,必须要减少 trySplit 的调用。 * * @apiNote * Even an inexact estimate is often useful and inexpensive to compute. * For example, a sub-spliterator of an approximately balanced binary tree * may return a value that estimates the number of elements to be half of * that of its parent; if the root Spliterator does not maintain an * accurate count, it could estimate size to be the power of two * corresponding to its maximum depth. 甚至一个不太精确的估算,也是有用的。 * * @return the estimated size, or {@code Long.MAX_VALUE} if infinite, * unknown, or too expensive to compute. */ long estimateSize();

getExactSizeIfKnown() 如果知道的话就会返回确定的大小。

/** * Convenience method that returns {@link #estimateSize()} if this * Spliterator is {@link #SIZED}, else {@code -1}. 如果Spliterator是SIZED的话, estimateSize就会返回确定的大小。 * @implSpec * The default implementation returns the result of {@code estimateSize()} * if the Spliterator reports a characteristic of {@code SIZED}, and * {@code -1} otherwise. * * @return the exact size, if known, else {@code -1}. */ default long getExactSizeIfKnown() { return (characteristics() & SIZED) == 0 ? -1L : estimateSize(); }

characteristics() 特性值。

/** * Returns a set of characteristics of this Spliterator and its * elements. The result is represented as ORed values from {@link * #ORDERED}, {@link #DISTINCT}, {@link #SORTED}, {@link #SIZED}, * {@link #NONNULL}, {@link #IMMUTABLE}, {@link #CONCURRENT}, * {@link #SUBSIZED}. Repeated calls to {@code characteristics()} on * a given spliterator, prior to or in-between calls to {@code trySplit}, * should always return the same result. 返回这个Spliterator的特性值的集合。 {@link#ORDERED}, {@link #DISTINCT}, {@link #SORTED}, {@link #SIZED}, {@link #NONNULL}, {@link #IMMUTABLE}, {@link #CONCURRENT}, {@link #SUBSIZED} 这8个,在下面有定义。 * * <p>If a Spliterator reports an inconsistent set of * characteristics (either those returned from a single invocation * or across multiple invocations), no guarantees can be made * about any computation using this Spliterator. * * @apiNote The characteristics of a given spliterator before splitting * may differ from the characteristics after splitting. For specific * examples see the characteristic values {@link #SIZED}, {@link #SUBSIZED} * and {@link #CONCURRENT}. 具体的例子看下面的说明。 * * @return a representation of characteristics */ int characteristics();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdfjj.html