Scala SortedSet forall()方法及示例
forall() 方法是用来检查给定的谓词是否满足排序集的所有元素。
方法定义: def forall(p: (A) => Boolean):Boolean
返回类型。如果所述谓词对排序集的所有元素都成立,则返回真,否则返回假。
例子 #1:
// Scala program of forall()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(3, 6, 12, 9, 21)
// Applying forall method
val result = s1.forall(y => {y % 3 == 0})
// Displays output
println(result)
}
}
输出。
true
例子#2。
// Scala program of forall()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(3, 7, 12, 9, 21)
// Applying forall method
val result = s1.forall(y => {y % 3 == 0})
// Displays output
println(result)
}
}
输出。
false
极客教程