bn.*_*bn. 5 java java-8 java-stream
下面的选项1或选项2是否正确(例如,一个优先于另一个)或它们是否相同?
选项1
collectionOfThings.
stream().
filter(thing -> thing.condition1() && thing.condition2())
Run Code Online (Sandbox Code Playgroud)
要么
选项2
collectionOfThings
.stream()
.filter(thing -> thing.condition1())
.filter(thing -> thing.condition2())
Run Code Online (Sandbox Code Playgroud)
要编译,第二个应该是
collectionOfThings.
stream().
filter(thing -> thing.condition1()).
filter(thing -> thing.condition2())
Run Code Online (Sandbox Code Playgroud)
它们都是等价的.有时一个比另一个更可读.
编写第二种方法的另一种方法是使用方法参考:
collectionOfThings
.stream()
.filter(Thing::condition1)
.filter(Thing::condition2)
Run Code Online (Sandbox Code Playgroud)
另请注意,惯例是将点放在行的开头而不是结尾,就像编写项目符号列表一样.
| 归档时间: |
|
| 查看次数: |
235 次 |
| 最近记录: |