Java Stream forEach()和forEachOrdered()方法

  • Java Stream forEach() and forEachOrdered() are terminal operations.

    Java Stream forEach()和forEachOrdered()是终端操作。
  • The forEach() method is used to perform an action on each elements of the stream.

    forEach()方法用于对流的每个元素执行操作。
  • If the forEach() method is used with parallel stream, the encounter order is not maintained. The action will be performed on each element, but their order will not be fixed.

    如果将forEach()方法与并行流一起使用,则不会维护遇到顺序。 该操作将在每个元素上执行,但是它们的顺序不会固定。
  • If you want to perform some action on stream elements in the encounter order, you should use forEachOrdered() method.

    如果要按遇到顺序对流元素执行某些操作,则应使用forEachOrdered()方法。
  • The underlying collection must have encounter order, otherwise forEachOrdered() will not be able to respect that. So using it with TreeSet or TreeMap is useless.

    基础集合必须具有遇到顺序,否则forEachOrdered()将无法遵守该顺序。 因此,将它与TreeSet或TreeMap一起使用是没有用的。
  • The forEachOrdered() action is executed on each stream element one by one, so it’s a bit slower than forEach() method.

    forEachOrdered()操作在每个流元素上一一执行,因此它比forEach()方法要慢一些。
  • Using forEachOrdered() on a parallel stream doesn’t provide benefits of parallel stream, so using it with a parallel stream doesn’t make much sense.

    在并行流上使用forEachOrdered()不能提供并行流的好处,因此在并行流上使用它没有多大意义。

Java Stream forEach()和forEachOrdered()方法签名 (Java Stream forEach() and forEachOrdered() Methods Signature)

void forEach(Consumer<? super T> action)

void forEachOrdered(Consumer&LT;? super T&GT; action);

Consumer is a Functional Interface. It represents an operation that accepts a single input argument and returns no result.

Consumer是一个功能接口 。 它表示一个接受单个输入参数且不返回结果的操作。

流forEach()示例 (Stream forEach() Example)

Let’s look at a simple example to print all the elements of the stream.

让我们看一个简单的示例,以打印流中的所有元素。

jshell> Stream<Integer> streamNumbers = Stream.of(1, 2, 3, 4);
streamNumbers ==> java.util.stream.ReferencePipeline$Head@5a10411

jshell> streamNumbers.forEach(System.out::println);
1
2
3
4

jshell>

并行流forEach()示例 (Parallel Stream forEach() Example)

Let’s see what happens when we use forEach() with a parallel stream.

让我们看看将forEach()与并行流一起使用会发生什么。

jshell> List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
list ==> [1, 2, 3, 4, 5, 6, 7]

jshell> list.parallelStream().forEach(System.out::println);
5
4
1
3
2
7
6

jshell>
Stream ForEach Example

Stream forEach() Example

流forEach()示例

Notice that the stream elements are getting in random order. The encounter order of the elements is not respected when forEach() is performing an action on stream elements.

请注意,流元素按随机顺序获取。 当forEach()对流元素执行操作时,将不遵守元素的遇到顺序。

并行流forEachOrdered()示例 (Parallel Stream forEachOrdered() Example)

Let’s see what happens when we use forEachOrdered() with a parallel stream of list elements.

让我们看看将forEachOrdered()与列表元素的并行流一起使用会发生什么。

jshell> List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
list ==> [1, 2, 3, 4, 5, 6, 7]

jshell> list.parallelStream().forEachOrdered(System.out::println);
1
2
3
4
5
6
7

jshell>

It’s clear that the encounter order of the stream is maintained.

很明显,流的遇到顺序得以保持。

参考 (Reference)

翻译自: https://www.journaldev.com/31841/java-stream-foreach-foreachordered-methods

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值