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 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 Scala Stack sum() method with example In Scala Stack class, the sum() method is utilized to return the sum of all the elements of the stack. Method Definition: def sum: A Return Type: It returns the sum of all the elements of the stack. Example #1: Scala // Scala program of sum() // method // Import Stack import scala.collection.mutable 1 min read Scala Stack size() method with example In Scala Stack class, the size() method is utilized to return the number of elements present in the stack. Method Definition: def size: Int Return Type: It returns the number of elements present in the stack. Example #1: Scala // Scala program of size() // method // Import Stack import scala.collect 1 min read Scala Set take() method with example The take() method is utilized to return a set consisting of first 'n' elements of the set. Method Definition: def take(n: Int): Set[A] Where 'n' is specifies the number of element to select. Return Type: It returns a set consisting of first 'n' elements of the set. Example #1: Scala // Scala program 1 min read Scala Stack find() method with example In Scala Stack class, the find() method is utilized to return an element that satisfies a given predicate in the stack. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element that satisfies a given predicate if present or else it returns None. Example 2 min read Scala Set drop() method with example The drop() method is utilized to delete the first ânâ elements or to return all elements except first 'n' elements. Method Definition: def drop(n: Int): Set[A]] Return Type: It returns all elements except first 'n' elements. Example #1: Scala // Scala program of drop() // method // Creating object o 1 min read Like