The Wayback Machine - https://web.archive.org/web/20210624163417/https://www.geeksforgeeks.org/scala-set-count-method-with-example/
Skip to content
Related Articles

Related Articles

Improve Article
Scala Set count() method with example
  • Last Updated : 15 Oct, 2019

The count() method is utilized to count the number of elements in the set.

Method Definition: def count(p: (A) => Boolean): Int

Return Type: It returns the number of elements present in the set.

Example #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) 
      
    } 
} 
Output:
5

Example #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) 
      
    } 
} 
Output:
0



My Personal Notes arrow_drop_up
Recommended Articles
Page :