Scala Mutable SortedSet count() method

Scala Mutable SortedSet count() method

在Scala中,可变集合中的 count() 方法用于计算SortedSet中元素的数量。

方法定义:def count(p: (A) => Boolean): Int

返回类型:返回SortedSet中存在的元素数。

示例1:

// Scala program of count()
// method
import scala.collection.mutable.SortedSet 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating SortedSets 
        val s1 = SortedSet(10, 22, 32, 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.mutable.SortedSet 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating SortedSets 
        val s1 = SortedSet(72, 666, 454) 
          
        // Applying count method 
        val result = s1.count(z=>true) 
          
        // Displays output 
        println(result) 
      
    } 
} 
3

阅读更多:Scala 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程