带有示例的 Scala ListSet count() 方法
在 Scala ListSet 中,count() 方法用于计算 listSet 中满足指定谓词的元素的数量。
Method Definition: def count(p: (A) => Boolean): ListSet[A]
Return Type: It returns the count of number of elements of the listset which satisfy the given predicate.
示例 1:
// Scala program of count()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.count(_>2)
// Displays output
println(result)
}
}
输出:
4
示例 2:
// Scala program of count()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.count(_<1)
// Displays output
println(result)
}
}
输出:
0