java .foreach 和 .stream().foreach 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29090655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between .foreach and .stream().foreach?
提问by ming
This is a example: code A:
这是一个例子:代码A:
files.forEach(f -> {
//TODO
});
and another code B may use on this way:
另一个代码 B 可能会以这种方式使用:
files.stream().forEach(f -> { });
What is the difference between both, with stream()
and no stream()
?
withstream()
和 no之间有什么区别stream()
?
回答by Stuart Marks
Practically speaking, they are mostly the same, but there is a small semantic difference.
实际上,它们大多相同,但在语义上存在细微差别。
Code A is defined by Iterable.forEach
, whereas code B is defined by Stream.forEach
. The definition of Stream.forEach
allows for the elements to be processed in any order -- even for sequential streams. (For parallel streams, Stream.forEach
will very likely process elements out-of-order.)
代码 A 由 定义Iterable.forEach
,而代码 B 由 定义Stream.forEach
。的定义Stream.forEach
允许以任何顺序处理元素——即使是顺序流。(对于并行流,Stream.forEach
很可能会乱序处理元素。)
Iterable.forEach
gets an Iterator from the source and calls forEachRemaining()
on it. As far as I can see, all current (JDK 8) implementations of Stream.forEach
on the collections classes will create a Spliterator built from one of the source's Iterators, and will then call forEachRemaining
on that Iterator -- just like Iterable.forEach
does. So they do the same thing, though the streams version has some extra setup overhead.
Iterable.forEach
从源获取一个迭代器并调用forEachRemaining()
它。据我所知,Stream.forEach
集合类上的所有当前(JDK 8)实现都将创建一个从源的迭代器之一构建的 Spliterator,然后将调用forEachRemaining
该迭代器——就像Iterable.forEach
那样。所以他们做同样的事情,尽管流版本有一些额外的设置开销。
However, in the future, it's possible that the streams implementation could change so that this is no longer the case.
但是,在未来,流实现可能会发生变化,因此不再是这种情况。
(If you want to guarantee ordering of processing streams elements, use forEachOrdered()
instead.)
(如果要保证处理流元素的顺序,请forEachOrdered()
改用。)
回答by Louis Wasserman
There is no difference in terms of semantics, though the direct implementation without stream
is probablyslightly more efficient.
有一个在语义方面没有差别,但直接执行,而不stream
是可能会更有效。
回答by Swathi
A stream is an sequence of elements (i.e a data structure) for using up an operation or iteration. Any Collectioncan be exposed as a stream. The operations you perform on a stream can either be
流是用于用完操作或迭代的元素序列(即数据结构)。任何集合都可以作为流公开。您对流执行的操作可以是
Intermediate operations(map, skip, concat, substream, distinct, filter, sorted, limit, peek..) producing another java.util.stream.Stream but the intermediate operations are lazy operations, which will be executed only after a terminal operationwas executed.
中间操作(映射,跳过,concat,则子,层次分明,过滤,排序,限制,PEEK ..)产生另一个java.util.stream.Stream但是中间操作是懒操作,这将仅一个之后执行终端操作是执行。
And the Terminal operations(forEach, max, count, matchAny, findFirst, reduce, collect, sum, findAny ) producing an object that is not a stream.
和终端业务(的forEach,最大,计数,matchAny,使用FindFirst,减少,收集,总之,findAny)生产的对象是不是甲流。
Basically it is similar to pipeline as in Unix.
基本上它类似于 Unix 中的管道。
回答by Unihedron
Both approaches uses the terminal operation Iterable.forEach
, but the version with .stream()
also unnecessarily creates a Stream
object representing the List. While there is no difference, it is suboptimal.
两种方法都使用终端操作Iterable.forEach
,但版本.stream()
也不必要地创建了一个Stream
表示列表的对象。虽然没有区别,但它是次优的。