java - 哪些操作保持顺序

标签 java java-8 java-stream

<分区>

TL;DR; 我正在寻找一个可以查找特定中间操作或终端操作的地方。在哪里可以找到此类文档?

编辑 这不是How to ensure order of processing in java8 streams? 的副本,因为该问题没有提供完整的操作列表。

背景

the package documentation说:

Whether or not a stream has an encounter order depends on the source and the intermediate operations

this excellent stackoverflow answer 中重复

In order to ensure maintenance of ordering throughout an entire stream operation, you have to study the documentation of the stream’s source, all intermediate operations and the terminal operation for whether they maintain the order or not (or whether the source has an ordering in the first place).

这一切都很好,但我应该查看哪个文档? the package documentation在示例中提到 map 保证排序,但它没有详尽的列表。 javadoc for the Stream class记录一些中间操作,但不是全部。 以map为例:

Returns a stream consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

filter

Returns a stream consisting of the elements of this stream that match the given predicate.

This is an intermediate operation.

这些都没有描述它们是否保留顺序。

This stackoverflow answer声明:

Actually every intermediate operation preserves an order by default. The only exceptions are:

  • unordered() which removes the ordering constraint.
  • sorted() which changes the order.

When it's not explicitly specified, you can assume that operation keeps the order. Even distinct() keeps the order, though it adds much complexity for parallel stream.

但是是否有任何官方文档来支持这一点?

加分 😉

实际上有两个单独的排序问题。

  1. 操作的输出是否保持与输入相同的顺序?
  2. 操作是否按顺序在每个元素上执行。

例如,并行 map 操作可以以任意顺序遍历所有元素(违反 2.),但仍保持返回流中的顺序(遵守 1.)

最佳答案

在对源代码进行一些研究之后,我总结了下表:

取自:Java streams - Part 6 - Spliterator

下表显示了允许修改字符的操作类型:

|                        | DISTICTS | SORTED | ORDERED | SIZED | SHORT_CIRCUIT |
| ---------------------- | -------- | ------ | ------- | ----- | --------------|
| source stream          | Y        | Y      | Y       | Y     | N             |
| intermediate operation | PCI      | PCI    | PCI     | PC    | PI            |
| terminal operation     | N        | N      | PC      | N     | PI            |
  • Y - 允许拥有
  • P - 梅蜜饯
  • C - 五月放晴。
  • 我 - 梅注入(inject)。
  • N - 无效;与操作无关。

取自Java streams - Stream methods characteristics table

下表显示了每个中间操作/终端操作可能打开和关闭的特性和标志:(SHORT_CIRCUIT 仅在StreamOpFlag 标志的上下文)

注意:P(保留)标志被添加到每个单元格中,除了带有 CI 的单元格(清除和注入(inject))标志。

|                  |  DISTINCT |  SORTED |  ORDERED |  SIZED |  SHORT_CIRCUIT |
| ---------------- | ----------| --------| ---------| -------| ---------------|
|  filter          |           |         |          |  C     |                |
|  forEach         |           |         |  C       |        |                |
|  forEachOrdered  |           |         |          |        |                |
|  allMatch        |           |         |  C       |        |  I             |
|  distinct        |  I        |         |          |  C     |                |
|  flatMap         |  C        |  C      |          |  C     |                |
|  anyMatch        |           |         |  C       |        |  I             |
|  collect         |           |         |          |        |                |
|  unOrdered       |           |         |  C       |        |                |
|  count           |  C        |  C      |  C       |  C     |                |
|  findAny         |           |         |  C       |        |  I             |
|  findFirst       |           |         |          |        |  I             |
|  flatMapToXXX    |  C        |  C      |          |  C     |                |
|  limit           |           |         |          |  C     |  I             |
|  map             |  C        |  C      |          |        |                |
|  mapToXXX        |  C        |  C      |          |        |                |
|  max             |           |         |          |        |                |
|  min             |           |         |          |        |                |
|  noneMatch       |           |         |  C       |        |  I             |
|  peek            |           |         |          |        |                |
|  reduce          |           |         |          |        |                |
|  skip            |           |         |  C       |  I     |                |
|  sorted          |           |  I      |  I       |        |                |
|  toArray         |           |         |          |        |                |
  • C - 清除。
  • I - 注入(inject)。

关于java - 哪些操作保持顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978604/

相关文章:

java - 如何将流结果存储到多个列表或映射中

java - JNDI 在服务器初始化函数中抛出 Servlet 异常

java - 尝试运行 Facelets 中嵌入的小程序时不兼容的魔法值 1010792557

java - 集合以流式传输到新集合

java - 根据字符串到长整型映射将定界字符串转换为定界长整型

java - 从 List 中删除第 N 个出现的元素

java - 如何有效管理各种转换任务?

java - 在自定义类上保存查询结果

java - 在 Java 8 中将 List<SomeObject> 转换为 List<AnotherObject>

java - 是否可以有一个带有中间生产者的 Java Stream?