Java 8 Thread Filter Intent Lazy Evaluation

Is option 1 or option 2 correct (for example, is one of them preferable to the other) or are they equivalent?

Option 1

collectionOfThings. stream(). filter(thing -> thing.condition1() && thing.condition2()) 

or

Option 2

 collectionOfThings .stream() .filter(thing -> thing.condition1()) .filter(thing -> thing.condition2()) 
+5
source share
1 answer

To compile, the second must be

 collectionOfThings. stream(). filter(thing -> thing.condition1()). filter(thing -> thing.condition2()) 

They are both equivalent. Sometimes one of them is more readable than the other.

An alternative way to write the second is to use a method reference:

 collectionOfThings .stream() .filter(Thing::condition1) .filter(Thing::condition2) 

Also note that the convention is to put a dot at the beginning of the line, and not at the end, since you would write a bulleted list.

+8
source

Source: https://habr.com/ru/post/1246785/


All Articles