Why is there no index in the Java 8 stream?

I want to know about Java 8 streams (stream < E >), which have the following methods:

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

What is the argument against not providing the following signature?

> forEachOrdered(BiConsumer< Integer,?super E> action)

>The index of the item in the stream and the item itself are then returned

With this overload, you can actually use indexes to prevent streams from being sorted

I'm really curious to see what arguments are against

Editing is actually applicable to iterators < E > and foreachremaining, and maybe more classes If no class provides such an option, I suspect it has been considered for Java 8 and rejected

Solution

Index each element needs to be assigned an index sequentially This defeats the point of parallel operations because each operation must be synchronized to get its index

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>