在Java 8流中没有索引的原因是什么? [英] What are the reasons for not having an index in Java 8 streams?

查看:90
本文介绍了在Java 8流中没有索引的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Java 8流( Stream< E> ),它们有以下方法:

I was wondering about the Java 8 streams (Stream<E>), they have the following methods:


  • forEach(Consumer<?super E> action)

  • forEachOrdered (消费者<?超级E>行动)

  • forEach(Consumer<? super E> action)
  • forEachOrdered(Consumer<? super E> action)

反对不提供以下签名的理由是什么?

What were the arguments against not supplying the following signature?


  • forEachOrdered(BiConsumer< Integer,?super E> 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.

    编辑,实际上同样适用于 Iterator< E> with forEachRemaining ,可能还有更多的类。

    如果没有类提供这样的opti on,然后我怀疑它已被考虑用于Java 8并被拒绝。

    Edit, the same actually holds for Iterator<E> with forEachRemaining, 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流中没有索引的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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