Scala Queue count() method with example Last Updated : 18 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The count() method is utilized to count the number of elements in the queue that satisfies a given predicate. Method Definition: def count(p: (A) => Boolean): Int Return Type: It returns the count the number of elements in the queue that satisfies a given predicate. Example #1: Scala // Scala program of count() // method // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a queue val q1 = Queue(1, 2, 3, 4, 5) // Print the queue println(q1) // Applying count method val result = q1.count(z => true) // Displays output print("Number of element in queue: " + result) } } Output: Queue(1, 2, 3, 4, 5) Number of element in queue: 5 Example #2: Scala // Scala program of count() // method // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a queue val q1 = Queue(1, 2, 3, 4, 5) // Print the queue println(q1) // Applying count method val result = q1.count(z => {z % 2 == 0}) // Displays output print("Number of element in queue divisible by 2: " + result) } } Output: Queue(1, 2, 3, 4, 5) Number of element in queue divisible by 2: 2 Comment More infoAdvertise with us Next Article Scala Queue count() method with example rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Similar Reads Scala Queue copyToArray() method with example The copyToArray() method is utilized in copying the elements of the queue to an Array. Method Definition: def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Int Parameters: xs: It denotes the array where elements are copied. start: It denotes the starting index for the copy to takes pla 2 min read Scala Set count() method with example 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 // Scala program of count() // method // Creating object object GfG { // Main method 1 min read Scala Map count() method with example The count() method is utilized to count pair of keys in the Map. Method Definition: def count(p: ((A, B)) => Boolean): Int Return Type: It returns the number of keys present in the map that satisfies the given predicate. Example #1: Scala // Scala program of count() // method // Creating object o 1 min read Scala Queue sum() method with example The sum() method is utilized to return the sum of all the elements of the queue. Method Definition: def sum: A Return Type: It returns the sum of all the elements of the queue. Example #1: Scala // Scala program of sum() // method // Import Queue import scala.collection.mutable._ // Creating object 1 min read Scala Queue map() method with example The map() method is utilized to build a new queue by applying a function to all elements of this queue. Method Definition: def map[B](f: (A) => B): Queue[B] Return Type: It returns a new queue containing all the elements after applying the given function. Example #1: Scala // Scala program of map 2 min read Like