检查集合中的所有项目是否与 Scala 中的谓词匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15932137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:09:42  来源:igfitidea点击:

Checking that all items in a collection match a predicate in Scala

scalapredicate

提问by ripper234

What's the most idiomatic way to test whether all items of a collection match a predicate?

测试集合的所有项目是否与谓词匹配的最惯用的方法是什么?

Any item?

任何项目?

回答by om-nom-nom

There are built-in functions for this:

为此有内置函数:

List(1,2,3,4).forall(x => x < 5)
res0: Boolean = true

for any:

对于任何:

List(1,2,3,4).exists(x => x > 3)
res1: Boolean = true