Scala Set count() 方法与示例
count()方法用于计算集合中元素的数量。
Method Definition: def count(p: (A) => Boolean): Int
Return Type: It returns the number of elements present in the set.
示例 #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