Open In App

Scala ListSet count() method with Example

Last Updated : 15 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In Scala ListSet, count() method is utilized to count the number of elements of the listSet which satisfy a stated predicate.

Method Definition: def count(p: (A) => Boolean): ListSet[A] Return Type: It returns the count of number of elements of the listset which satisfy the given predicate. 

Example 1: 

Scala
// Scala program of count() 
// method 
import scala.collection.immutable._
// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
    
        // Creating listset
        val m1 = ListSet(1, 2, 3, 4, 5, 7)
        
        // Applying filter method 
        val result = m1.count(_>2) 
        
        // Displays output 
        println(result) 
    
    } 
} 
Output:
4

Example 2: 

Scala
// Scala program of count() 
// method 
import scala.collection.immutable._
// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
    
        // Creating listset
        val m1 = ListSet(1, 2, 3, 4, 5, 7)
        
        // Applying filter method 
        val result = m1.count(_<1) 
        
        // Displays output 
        println(result) 
    
    } 
} 
Output:
0

Next Article

Similar Reads