Scala Set count()方法及示例
count() 方法是用来计算集合中的元素数量的。
方法定义: def count(p: (A) => Boolean):Int
返回类型。它返回集合中存在的元素数量。
例子 #1:
// Scala program of count()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating sets
val s1 = Set(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
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating sets
val s1 = Set()
// Applying count method
val result = s1.count(z=>true)
// Displays output
println(result)
}
}
输出。
0
极客教程