How to query a sequence in Scala

1 Answer

0 votes
object QuerySeq {
  def main(args: Array[String]): Unit = {
    val seq = Seq(1, 6, 17, 37, 38, 44)

    // Checks if the sequence contains an element.
    val exists = seq.contains(17) 
    println(exists)  

    // isEmpty and nonEmpty: Checks if the sequence is empty or not.
    val emptyCheck = seq.isEmpty 
    println(emptyCheck)  

    // Counts elements satisfying a predicate.
    val evenCount = seq.count(_ % 2 == 0) 
    println(evenCount)  
  }
}



/*
run:

true
false
3

*/

 



answered Nov 15, 2025 by avibootz
...