Scala Mutable SortedSet foreach() method Last Updated : 26 Mar, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In Scala mutable collections, SortedSet foreach() method is utilized to apply the given function to all the elements of the SortedSet. Method Definition: def foreach(f: (A) => Unit): Unit Return Type: It returns all the elements of the SortedSet after applying the given function to each of them. Example #1: Scala // Scala program of foreach() // method import scala.collection.mutable.SortedSet // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a SortedSet val s1 = SortedSet(13, 22, 19, 21) // Applying foreach method s1.foreach(x => println(x)) } } Output: 13 19 21 22 Example #2: Scala // Scala program of foreach() // method import scala.collection.mutable.SortedSet // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a SortedSet val s1 = SortedSet(5, 7, 8, 3, 2) // Applying foreach method s1.foreach(x => println(x + " times " + x + " = " + x*x)) } } Output: 2 times 2 = 4 3 times 3 = 9 5 times 5 = 25 7 times 7 = 49 8 times 8 = 64 Comment More infoAdvertise with us Next Article Scala mutable SortedSet &() method S Shivam_k Follow Improve Article Tags : Scala Scala Scala-Method Scala mutable-sortedset Similar Reads Scala Mutable SortedSet forall() method In Scala mutable collections, SortedSet forall() method is utilized to check if the given predicate satisfies all the elements of the SortedSet or not. Method Definition: def forall(p: (A) => Boolean): Boolean Return Type: It returns true if the stated predicate holds true for all the elements of 1 min read Scala Mutable SortedSet head() method In Scala mutable collections, SortedSet head() method is utilized to display the first element of the SortedSet. Method Definition: def head: A Return Type: It returns the first element of the SortedSet. Example #1: Scala // Scala program of head() // method import scala.collection.mutable.SortedSet 1 min read Scala Mutable SortedSet map() method In Scala mutable collections, map() method is utilized to build a new SortedSet by applying a function to all elements of this SortedSet. Method Definition: def map[B](f: (A) => B): mutable.SortedSet[B] Return Type: It returns a new SortedSet containing all the elements after applying the given f 1 min read Scala mutable SortedSet &() method In Scala mutable collection, &() method is utilized to create a new SortedSet consisting of all elements that are present in both the given SortedSets. Method Definition: def &(that: SortedSet[A]): SortedSet[A] Return Type: It returns a new SortedSet consisting of all elements that are prese 1 min read Scala mutable SortedSet &~() method In Scala mutable collection &~() method is utilized to create a new SortedSet consisting of elements after the difference between two given SortedSets. Method Definition: def &~(that: SortedSet[A]): SortedSet[A] Return Type: It returns a TreeSet consisting of elements after the difference be 1 min read Scala Mutable SortedSet max() method In Scala mutable collections, max() method is utilized to find the largest element of all the elements in the SortedSet. Method Definition: def max: A Return Type: It returns the largest of all the elements in the SortedSet. Example #1: Scala // Scala program of max() // method import scala.collecti 1 min read Like