Scala immutable TreeSet count() method Last Updated : 19 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In Scala immutable 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. Example #1: Scala // Scala program of count() // method // Import TreeSet import scala.collection.immutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating TreeSet val t1 = TreeSet(2, 1, 3, 4, 5) // Print the TreeSet println(t1) // Applying count() method val result = t1.count(x => {x % 2 == 0}) // Display output print("Number of even element in the TreeSet: " + result) } } Output: TreeSet(1, 2, 3, 4, 5) Number of even element in the TreeSet: 2 Example #2: Scala // Scala program of count() // method // Import TreeSet import scala.collection.immutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating TreeSet val t1 = TreeSet(2, 1, 3, 4, 5) // Print the TreeSet println(t1) // Applying count() method val result = t1.count(x => {x % 2 != 0}) // Display output print("Number of odd element in the TreeSet: " + result) } } Output: TreeSet(1, 2, 3, 4, 5) Number of odd element in the TreeSet: 3 Comment More infoAdvertise with us Next Article Scala immutable TreeSet clone() method S Shivam_k Follow Improve Article Tags : Scala Scala immutable-TreeSet Similar Reads Scala immutable TreeSet clone() method In Scala immutable TreeSet class, the clone() method is used to create a copy of the given TreeSet. Method Definition: def clone(): Queue[A] Return Type: It return a new TreeSet which is a copy of the given TreeSet. Example #1: Scala // Scala program of clone() // method // Import TreeSet import sca 1 min read Scala immutable TreeSet contains() method In Scala immutable TreeSet class, the contains() method is utilized to check if an element is present in the TreeSet of not. Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the TreeSet or else it returns false. Example #1: Scala // Scala pr 1 min read Scala immutable TreeSet --() method In Scala immutable TreeSet class, the --() method is utilised to subtract elements of one TreeSet to another TreeSet. Method Definition: def --(that: IterableOnce[A]): TreeSet[A] Return Type: It returns a new TreeSet which contains the element from A - B TreeSets. Example #1: Scala // Scala program 2 min read Scala immutable TreeSet +() method In Scala immutable TreeSet class, the +() method is utilised to add an element to the TreeSet unless it is already present. Method Definition: def +(elem: A): TreeSet[A] Return Type: It returns a new TreeSet with an additional element unless the element is already present. Example #1: Scala // Scala 2 min read Scala immutable TreeSet ++() method In Scala immutable TreeSet class, the ++() method is utilised to add elements of one TreeSet to another TreeSet. Method Definition: def ++(that: IterableOnce[A]): TreeSet[A] Return Type: It returns a new TreeSet which contains all the element from both the given TreeSets. Example #1: Scala // Scala 2 min read Scala immutable TreeSet -() method In Scala immutable TreeSet class, the -() method is utilized to remove an element from the given TreeSet. Method Definition: def -(elem: A): TreeSet[A] Return Type: It returns a new TreeSet with an element removed from the given TreeSet. Example #1: Scala // Scala program of -() // method // Import 1 min read Like