Monday 16 May 2022

Scala Function - Count

Count defines how many elements in a given collection satisfy the input predicate.

 def countExample(): Unit = {
    def isEven = (input: Int) => (input % 2 == 0)
    val numberList = List(1,2,3,4,5,6,7,8,9,10)
    val totalEvenNumbersInList =numberList.count(isEven)
    println(s"Total Even Numbers in List = ${totalEvenNumbersInList}") //5
  }





No comments:

Post a Comment