不可分裂的分裂器 [英] Unsplittable Spliterators

查看:199
本文介绍了不可分裂的分裂器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Spliterator 的工作方式,以及如何设计分裂器。我认识到 trySplit()可能是 Spliterator 的一个更重要的方法,但当我看到第三个 - 派对 Spliterator 实现,有时我看到他们的分裂器无条件地为 trySplit()返回null。

I'm trying to understand how Spliterator works, and how spliterators are designed. I recognize that trySplit() is likely one of the more important methods of Spliterator, but when I see some third-party Spliterator implementations, sometimes I see that their spliterators return null for trySplit() unconditionally.

问题:


  1. 普通迭代器和之间是否存在差异? Spliterator 无条件地返回null?看起来这样的分裂者会破坏分裂。

  2. 当然,有一些合法的分裂器用例在 trySplit()上有条件地返回null ,但是有没有一个无条件返回null的分裂器的合法用例?

  1. Is there a difference between an ordinary iterator and a Spliterator that returns null unconditionally? It seems like such a spliterator defeats the point of, well, splitting.
  2. Of course, there are legitimate use cases of spliterators that conditionally return null on trySplit(), but is there a legitimate use case of a spliterator that unconditionally returns null?


推荐答案

虽然Spliterator优于Iterator的主要优点是,正如你所说的,它的trySplit()方法允许它并行化,但还有其他显着优势:

While the main advantage of Spliterator over Iterator is, as you said, its trySplit() method which allows it to be parallelized, there are other significant advantages:


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

Spliterator API旨在支持高效并行除顺序遍历之外的遍历,通过支持分解以及单元素迭代。 此外,通过Spliterator访问元素的协议旨在实现比Iterator更小的每元素开销,并避免使用hasNext()和next()的单独方法所固有的竞争。

The Spliterator API was designed to support efficient parallel traversal in addition to sequential traversal, by supporting decomposition as well as single-element iteration. In addition, the protocol for accessing elements via a Spliterator is designed to impose smaller per-element overhead than Iterator, and to avoid the inherent race involved in having separate methods for hasNext() and next().

此外,可以使用 StreamSupport.stream ,以利用Java8的流。

Furthermore, Spliterators can be directly converted to Streams using StreamSupport.stream to make use of Java8's streams.

这篇关于不可分裂的分裂器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆