Scala Stack count() method with example Last Updated : 03 Nov, 2019 Comments Improve Suggest changes Like Article Like Report In Scala Stack class, the count() method is utilized to count the number of elements in the stack 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 stack that satisfies a given predicate. Example #1: Scala // Scala program of count() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a stack val s1 = Stack(1, 2, 3, 4, 5) // Print the stack println(s1) // Applying count method val result = s1.count(z => true) // Displays output print("Number of element in stack: " + result) } } Output: Stack(1, 2, 3, 4, 5) Number of element in stack: 5 Example #2: Scala // Scala program of count() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a stack val s1 = Stack(1, 2, 3, 4, 5) // Print the stack println(s1) // Applying count method val result = s1.count(z => {z % 2 == 0}) // Displays output print("Number of element in stack divisible by 2: " + result) } } Output: Stack(1, 2, 3, 4, 5) Number of element in stack divisible by 2: 2 Comment More infoAdvertise with us Next Article Scala Stack count() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Similar Reads 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 SortedMap count() method with example The count() method is utilized to count pair of keys in the SortedMap. Method Definition: def count(p: ((A, B)) => Boolean): Int Return Type: It returns the number of keys present in the SortedMap that satisfies the given predicate. Example #1: Scala // Scala program of count() // method import s 1 min read Scala TreeSet count() method with example In Scala TreeSet class, the count() method is utilized to count the number of elements in the TreeSet 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 TreeSet that satisfies a given predicate. Ex 2 min read Scala SortedSet count() method with example The count() method is utilized to count the number of elements in the SortedSet. Method Definition: def count(p: (A) => Boolean): Int Return Type: It returns the number of elements present in the SortedSet. Example #1: Scala // Scala program of count() // method import scala.collection.immutable. 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 Like