What are the reasons for not having an index in Java 8 streams?(Java 8 流中没有索引的原因是什么?)
问题描述
我想知道 Java 8 流 (Stream<E>
),它们有以下方法:
I was wondering about the Java 8 streams (Stream<E>
), they have the following methods:
forEach(Consumeraction)
forEachOrdered(Consumeraction)
反对不提供以下签名的理由是什么?
What were the arguments against not supplying the following signature?
forEachOrdered(BiConsumer
action) - 然后它将返回流中项目的索引和项目本身.
有了这个重载,就可以在流被排序的情况下实际使用索引.
With this overload it would be possible to actually use the index in case the stream was ordered.
我真的很想知道反对它的论点.
I am really curious to see what the arguments are against it.
Edit,实际上同样适用于带有
forEachRemaining
的Iterator
以及可能更多的类.
如果没有一个类提供这样的选项,那么我怀疑它已被考虑用于 Java 8 并被拒绝.Edit, the same actually holds for
Iterator<E>
withforEachRemaining
, and possibly more classes.
If none of the classes provide such option, then I suspect it has been considered for Java 8 and denied.推荐答案
索引每个元素需要顺序分配索引.这将破坏并行操作的意义,因为每个操作都必须同步才能获得其索引.
indexing every element requires a sequential assignment of the indexes. this would defeat the point of parallel operations, since each operation would have to synchronize to get its index.
这篇关于Java 8 流中没有索引的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 8 流中没有索引的原因是什么?
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01