Scala SortedSet count()方法及示例
count() 方法是用来计算SortedSet中的元素数量的。
方法定义: def count(p: (A) => Boolean):Int
返回类型。返回排序集中存在的元素的数量。
例子 #1:
// Scala program of count()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedSets
val s1 = SortedSet(1, 2, 3, 4, 5)
// Applying count method
val result = s1.count(z=>true)
// Displays output
println(result)
}
}
输出。
5
例子#2。
// Scala program of count()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedSets
val s1 = SortedSet(7, 6, 4)
// Applying count method
val result = s1.count(z=>true)
// Displays output
println(result)
}
}
输出。
3
极客教程