Scala Iterator count() method with example Last Updated : 30 Jun, 2019 Comments Improve Suggest changes Like Article Like Report The count() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the class IterableOnceOps. It counts the number of elements in the stated collection which satisfy the given predicate. Method Definition : def count(p: (A) => Boolean): Int Where, p is the predicate used. Return Type :It returns the number of elements satisfying the predicate p. Example #1: Scala // Scala program of count() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating an Iterator val iter = Iterator(5, 6, 8) // Applying count method val result = iter.count(x => {x % 3 != 0}) // Displays output println(result) } } Output: 2 Here, only two elements satisfies the stated predicate so, two is returned. Example #2: Scala // Scala program of count() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating an Iterator val iter = Iterator(4, 6, 10, 11, 13) // Applying count method val result = iter.count(x => {x % 2 == 0}) // Displays output println(result) } } Output: 3 Comment More infoAdvertise with us Next Article Scala Iterator count() method with example N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Scala ListSet count() method with Example In Scala ListSet, count() method is utilized to count the number of elements of the listSet which satisfy a stated predicate. Method Definition: def count(p: (A) => Boolean): ListSet[A] Return Type: It returns the count of number of elements of the listset which satisfy the given predicate. Examp 1 min read Scala Iterator drop() method with example The drop() method belongs to the concrete value member of the class Abstract Iterator. It is defined in the classes Iterator and IterableOnceOps. It is utilized to move the iterator n places ahead and if n is greater than the iteratorâs length, then it will throw an exception. Method Definition : de 2 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 Iterator duplicate() method with example The duplicate() method belongs to the concrete value member of the class iterator. It generates a duplicate of the iterator which will iterate over the alike order of values. The duplicate iterators are said to be equal if they are put at the identical element. Method Definition: def duplicate: (Ite 1 min read Scala Iterator seq() method with example The seq() method belongs to the concrete value members of the class Iterable. It is helpful in visualizing the sequential view of the stated collection. It has a time complexity of O(1). Method Definition: def seq: Iterator[A] Return Type: It returns a sequential view of the iterator. Example : Scal 1 min read Like