findAny和findFirst区别

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/huanghanqian/article/details/102807972
收起

java
专栏收录该内容
18 篇文章0 订阅
订阅专栏
试验了一下java stream中的findAny和findFirst,发现都返回了列表中的第一个元素。那么,这两种方法的区别是什么呢?

查看了一下Java API document:

findFirst:

findFirst

Optional<T> findFirst()
Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.

This is a short-circuiting terminal operation.

Returns:

an Optional describing the first element of this stream, or an empty Optional if the stream is empty

Throws:

NullPointerException - if the element selected is null

顾名思义,即返回列表中的第一个元素。

这里的short-circuiting是指:有时候需要在遍历中途停止操作,比如查找第一个满足条件的元素或者limit操作。在Stream中short-circuiting操作有:anyMatch、allMatch、noneMatch、findFirst、findAny、limit,这些操作在Sink中都有一个变量来判断是否短路,比如limit用的是m,match用的是stop,find用的是hasValue。

这里的terminal operation是指:一个终结操作,比如foreach,IntStream.sum

那么,findAny是什么呢?

findAny

Optional<T> findAny()
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

This is a short-circuiting terminal operation.

The behavior of this operation is explicitly nondeterministic(不确定的); it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations(调用) on the same source may not return the same result. (If a stable result is desired, use findFirst() instead.)

Returns:

an Optional describing some element of this stream, or an empty Optional if the stream is empty

Throws:

NullPointerException - if the element selected is null

See Also:

findFirst()

可以看到findAny()操作,返回的元素是不确定的,对于同一个列表多次调用findAny()有可能会返回不同的值。使用findAny()是为了更高效的性能。如果是数据较少,串行地情况下,一般会返回第一个结果,如果是并行的情况,那就不能确保是第一个。比如下面的例子会随机地返回OptionalInt[50]。

System.out.println(IntStream.range(0, 100).parallel().findAny());
让我们来举另外一个例子:

List<String> lst1 = Arrays.asList("Jhonny", "David", "Jack", "Duke", "Jill","Dany","Julia","Jenish","Divya");
List<String> lst2 = Arrays.asList("Jhonny", "David", "Jack", "Duke", "Jill","Dany","Julia","Jenish","Divya");
 
Optional<String> findFirst = lst1.parallelStream().filter(s -> s.startsWith("D")).findFirst();
Optional<String> fidnAny = lst2.parallelStream().filter(s -> s.startsWith("J")).findAny();
 
System.out.println(findFirst.get()); //总是打印出David
System.out.println(fidnAny.get()); //会随机地打印出Jack/Jill/Julia
参考链接:

1. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#findFirst--

2. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#findAny--

3. https://stackoverflow.com/questions/35359112/difference-between-findany-and-findfirst-in-java-8
————————————————
版权声明:本文为CSDN博主「huanghanqian」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/huanghanqian/article/details/102807972

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值