lambda -- Java 8 find first element by predicate

 

 

i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjoxODY2MjQsImNoIjoxMTc4LCJjciI6NjYyODEyLCJkaSI6IjE0MzY5MzU2ZDYyNjQ1MzU5MTQ4MWU0ZGZiNDFiZTYyIiwiZG0iOjEsImZjIjo2ODQ1MzQsImZsIjo0MTAxODMsImlwIjoiMTE4LjE5NC4yMzYuOSIsImt3IjoiamF2YSxqYXZhLTgiLCJudyI6MjIsInBjIjowLCJwciI6MTU2OCwicnQiOjEsInN0Ijo4Mjc3LCJ1ayI6InVlMS0wOTUwZWI1ODgxNDU0YTZlOTViNGMxYzk1ZWZhYWZlNiIsInpuIjo0MywidHMiOjE0MTYwNDM5NTMwNzcsImJmIjp0cnVlfQ&s=3SZy4Njyff24Y7fVA29LkEXuIDI

I've just started playing with Java 8 lambdas and I'm trying to implement some of the things that I'm used to in functional languages.

For example, most functional languages have some kind of find function that operates on sequences, or lists that returns the first element, for which the predicate is true. The only way I can see to achieve this in Java 8 is:

lst.stream()
    .filter(x -> x > 5)
    .findFirst()

However this seems inefficient to me, as the filter will scan the whole list, at least to my understanding (which could be wrong). Is there a better way?

share | improve this question
   
It's not inefficient, Java 8 Stream implementation is lazy evaluated, so filter is applied only to terminal operation. Same question here:stackoverflow.com/questions/21219667/stream-and-lazy-evaluation    Marek Gregor May 16 at 13:35  
   
Cool. That's what I hoped it'd do. It would've been a major design flop otherwise.    siki May 16 at 13:52

2 Answers

up vote 12 down vote accepted

No filter does not scan the whole stream. It's an intermediate operation, which returns a lazy stream (actually all intermediate operations return a lazy stream). To convince you, you can simply do:

List<Integer> list = new ArrayList<>(Arrays.asList(1,10,3,7,5));
int a = list.stream().filter(x -> {System.out.println("filtered"); return x > 5;}).findFirst().get();
System.out.println(a);

Which outputs:

filtered
filtered
10

You see that only the two first elements of the stream are actually processed.

So you can go with your approach which is perfectly fine.

share | improve this answer
3 
list.stream().peek(System.out::println).filter(x -> x>5).findFirst()will demonstrate it as well…    Holger   May 19 at 8:42

However this seems inefficient to me, as the filter will scan the whole list

No it won't - it will "break" as soon as the first element satisfying the predicate is found. You can read more about laziness in the stream package javadoc, in particular (emphasis mine):

Many stream operations, such as filtering, mapping, or duplicate removal, can be implemented lazily, exposing opportunities for optimization. For example, "find the first String with three consecutive vowels" need not examine all the input strings. Stream operations are divided into intermediate (Stream-producing) operations and terminal (value- or side-effect-producing) operations. Intermediate operations are always lazy.

share | improve this answer

转载于:https://www.cnblogs.com/wych/p/4099701.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值