lambda -- Java 8 find first element by predicate

 

 

Java 8 find first element by predicate


up vote6down votefavorite

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?

java java-8


share|improve this question

asked May 16 at 13:28

siki

509313

   

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 GregorMay 16 at 13:35 
   

Cool. That‘s what I hoped it‘d do. It would‘ve been a major design flop otherwise. –  sikiMay 16 at 13:52

add a comment

2 Answers

activeoldestvotes


up vote12down voteaccepted

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

edited May 16 at 13:48

answered May 16 at 13:37

ZouZou

24.3k52752

3  

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

add a comment


up vote2down vote

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
时间: 2024-12-29 03:30:10

lambda -- Java 8 find first element by predicate的相关文章

Lambda&Java多核编程-6-方法与构造器引用

在Lambda&Java多核编程-2-并行与组合行为一文中,我们对Stream<Contact>里的每一位联系人调用call()方法,并根据能否打通的返回结果过滤掉已经失效的项. 应该注意到此时filter(..)中Lambda的写法有些特殊: // ....filter(Contact::call)// ... 按常理我们应该使用s -> s.call(),但是这里却将参数.箭头以及对参数调用方法全部用其类型Contact的方法标签(暂且这样称呼)call来代替,而这个::就跟

[Java 8 Lambda] java.util.stream 简介

包结构如下所示: 这个包的结构很简单,类型也不多. BaseStream接口 所有Stream接口类型的父接口,它继承自AutoClosable接口,定义了一些所有Stream都具备的行为. 因为继承自AutoClosable接口,所以所有的Stream类型都可以用在Java 7中引入的try-with-resource机制中,以达到自动关闭资源的目的.实际上,只有当Stream是通过Socket,Files IO等方式创建的时候,才需要关闭它.对于来自于Collections,Arrays的S

[Java 8 Lambda] java.util.stream 简单介绍

包结构例如以下所看到的: 这个包的结构非常easy,类型也不多. BaseStream接口 全部Stream接口类型的父接口,它继承自AutoClosable接口,定义了一些全部Stream都具备的行为. 由于继承自AutoClosable接口,所以全部的Stream类型都能够用在Java 7中引入的try-with-resource机制中,以达到自己主动关闭资源的目的.实际上,仅仅有当Stream是通过Socket,Files IO等方式创建的时候,才须要关闭它.对于来自于Collection

java集合(3)-Java8新增的Predicate操作集合

Java8起为Collection集合新增了一个removeIf(Predicate filter)方法,该方法将批量删除符合filter条件的所有元素.该方法需要一个Predicate(谓词)对象作为参数,Predicate也是函数式接口,因此可以使用Lambda表达式作为参数. package com.j1803.collectionOfIterator; import java.util.Collection; import java.util.HashSet; public class

Java—Java 8 新增特性详解(Predicate和Stream)

Predicate接口 Predicate接口介绍 ??Predicate是函数式接口,可以使用Lambda表达式作为参数.Java 8为集合Collection新增了removeIf(Predicate filter)方法,可以批量删除符合filter条件的所有元素. Predicate接口使用范例 测试Collection的removeIf()方法. 示例1 1)运行类: public class DemoApplication { public static void main(Strin

selenium 调用JS操作滚动条(java)来解决element not clickable的问题

今天在运行自动化用例的时候,发现总是某个元素提示not  clickable.分析原因有可能是页面右下角那个大大的top图标,刚好挡住了我要点击的元素.要解决就得拉动页面,就需要操作页面上的滚动条. 使用了一个比较简单的方法,在这里记录下来: JavascriptExecutor JS=(JavascriptExecutor) driver; String high="scroll(0,10000);";//滚动到Y值10000像素的位置,一般10000就到页面的底部了,可以根据自己需

【Java学习笔记之三十一】详解Java8 lambda表达式

Java 8 发布日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Java 8之前,如果想将行为传入函数,仅有的选择就是匿名类,需要6行代码.而定义行为最重要的那行代码,却混在中间不够突出.Lambda表达式取代了匿名类,取消了模板,允许用函数式风格编写代码.这样有时可读性更好,表达更清晰.在Java生态系统中,函数式表达与对面向对象的全面支持是个激动人心的进步.将进一步促进并行

Java 8 Lambda表达式10个示例【存】

PS:不能完全参考文章的代码,请参考这个文件http://files.cnblogs.com/files/AIThink/Test01.zip 在Java 8之前,如果想将行为传入函数,仅有的选择就是匿名类,需要6行代码.而定义行为最重要的那行代码,却混在中间不够突出.Lambda表达式取代了匿名类,取消了模板,允许用函数式风格编写代码.这样有时可读性更好,表达更清晰.在Java生态系统中,函数式表达与对面向对象的全面支持是个激动人心的进步.将进一步促进并行第三方库的发展,充分利用多核CPU.尽

Java 8 lambda表达式20180404

Java 8 lambda表达式示例 我个人对Java 8发布非常激动,尤其是lambda表达式和流API.越来越多的了解它们,我能写出更干净的代码.虽然一开始并不是这样.第一次看到用lambda表达式写出来的Java代码时,我对这种神秘的语法感到非常失望,认为它们把Java搞得不可读,但我错了.花了一天时间做了一些lambda表达式和流API示例的练习后,我开心的看到了更清晰的Java代码.这有点像学习泛型,第一次见的时候我很讨厌它.我甚至继续使用老版Java 1.4来处理集合,直到有一天,朋