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

引入Example,跟源码。

public static void main(String[] args) { List<String> list = Arrays.asList("hello", "world", "hello world"); list.stream().forEach(System.out::println); } 1. stream()

来自Collection接口中的默认方法。

/** * Returns a sequential {@code Stream} with this collection as its source. * * <p>This method should be overridden when the {@link #spliterator()} * method cannot return a spliterator that is {@code IMMUTABLE}, * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} * for details.) 当这个 spliterator()无法返回这三个(不可变的,并行的,延迟绑定的)类型中的一个的话, 这个方法需要被重写。 * * @implSpec * The default implementation creates a sequential {@code Stream} from the * collection's {@code Spliterator}. 返回一个针对于当前元素的一个串行流。 * * @return a sequential {@code Stream} over the elements in this collection * @since 1.8 */ default Stream<E> stream() { return StreamSupport.stream(spliterator(), false); }

所以,这里就要先了解一下spliterator()这个方法是怎么实现的。

spliterator()的源码实现

实现方法和stream()一样,在Collection接口中的默认方法

/** * Creates a {@link Spliterator} over the elements in this collection. * * Implementations should document characteristic values reported by the * spliterator. Such characteristic values are not required to be reported * if the spliterator reports {@link Spliterator#SIZED} and this collection * contains no elements. Spliterator#SIZED,集合,固定大小,并且没有值。的时候是不用报告的。 --备注:和collectors的characteristic特性值 * * <p>The default implementation should be overridden by subclasses that * can return a more efficient spliterator. In order to * preserve expected laziness behavior for the {@link #stream()} and * {@link #parallelStream()}} methods, spliterators should either have the * characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be * <em><a href="Spliterator.html#binding">late-binding</a></em>. * If none of these is practical, the overriding class should describe the * spliterator's documented policy of binding and structural interference, * and should override the {@link #stream()} and {@link #parallelStream()} * methods to create streams using a {@code Supplier} of the spliterator, * as in: 默认的实现,应该被子类所重写。为了保留期望的stream()的延迟行为。分割迭代器的特性值 只有在满足{@code IMMUTABLE} or {@code CONCURRENT}的时候,才是具有延迟行为的。 如果上面条件都无法做的话,重写的类应该去描述这个分割迭代器的文档 并且重写。 用下面的这种方式去定义。 * <pre>{@code * Stream<E> s = StreamSupport.stream(() -> spliterator(), spliteratorCharacteristics) * }</pre> * <p>These requirements ensure that streams produced by the * {@link #stream()} and {@link #parallelStream()} methods will reflect the * contents of the collection as of initiation of the terminal stream * operation. 这些要求确保了由这两个方法生成的流,反应了流的内容 (在终止流操作执行的一瞬间) * * @implSpec * The default implementation creates a * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator * from the collections's {@code Iterator}. The spliterator inherits the * <em>fail-fast</em> properties of the collection's iterator. 默认的实现 从集合的迭代器中,创建一个延迟绑定的分割迭代器。 分割迭代器会继承迭代器的快速失败的属性。 * <p> * The created {@code Spliterator} reports {@link Spliterator#SIZED}. 创建的分割迭代器,会携带一个 Spliterator#SIZED (固定大小的)的特性值 * * @implNote * The created {@code Spliterator} additionally reports * {@link Spliterator#SUBSIZED}. 还会额外的增加一个Spliterator#SUBSIZED(子大小)的确定。 * * <p>If a spliterator covers no elements then the reporting of additional * characteristic values, beyond that of {@code SIZED} and {@code SUBSIZED}, * does not aid clients to control, specialize or simplify computation. * However, this does enable shared use of an immutable and empty * spliterator instance (see {@link Spliterators#emptySpliterator()}) for * empty collections, and enables clients to determine if such a spliterator * covers no elements. 如果分割迭代器里面没有元素,那么除了 {@code SIZED} and {@code SUBSIZED}之外其他的特性,对于计算的控制是没有帮助作用的。 不过可以促进空的迭代器的共享使用。 参见: Spliterators#emptySpliterator()、 对于一个空的迭代器可以判断是不是没有元素 * * @return a {@code Spliterator} over the elements in this collection * @since 1.8 */ @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, 0); }

那么,到底什么是分割迭代器? Spliterator

到底什么是分割迭代器 —— Spliterator类

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

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