Scala SortedMap foreach() method with example Last Updated : 03 Nov, 2019 Comments Improve Suggest changes Like Article Like Report The foreach() method is utilized to apply the given function to all the elements of the SortedMap. Method Definition: def foreach(f: ((A, B)) => Unit): Unit Return Type: It returns all the elements of the SortedMap after applying the given function to each of them. Example #1: Scala // Scala program of foreach() // method import scala.collection.immutable.SortedMap // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating SortedMap val m1 = SortedMap(3 -> "geeks", 1 -> "for", 2 -> "cs", 6 -> "geeks") // Applying foreach method and // displaying output val result = m1.foreach(x => println("key=" + x._1 + ", value=" + x._2)) } } Output: key=1, value=for key=2, value=cs key=3, value=geeks key=6, value=geeks Example #2: Scala // Scala program of foreach() // method import scala.collection.immutable.SortedMap // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating SortedMap val m1 = SortedMap(3 -> "geeks", 1 -> "for", 2 -> "cs", 3 -> "geeks") // Applying foreach method and // displaying output val result = m1.foreach(x => println("key=" + x._1 + ", value=" + x._2)) } } Output: key=1, value=for key=2, value=cs key=3, value=geeks So, the identical elements are taken only once. Comment More infoAdvertise with us Next Article Scala SortedMap foreach() method with example G gopaldave Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Scala SortedSet foreach() method with example The 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 o 1 min read Scala Mutable SortedMap foreach() method with example The foreach() method is utilized to apply the given function to all the elements of the SortedMap. Method Definition: def foreach(f: ((A, B)) => Unit): Unit Return Type: It returns all the elements of the SortedMap after applying the given function to each of them. Example #1: Scala // Scala prog 1 min read Scala Set foreach() method with example The foreach() method is utilized to apply the given function to all the elements of the set. Method Definition: def foreach(f: (A) => Unit): Unit Return Type: It returns all the elements of the set after applying the given function to each of them. Example #1: Scala // Scala program of foreach() 1 min read Scala Stack foreach() method with example In Scala Stack class, the foreach() method is utilized to apply a given function to all the elements of the stack. Method Definition: def foreach[U](f: (A) => U): Unit Return Type: It returns all the elements of the stack after applying the given function to each of them. Example #1: Scala // Sca 2 min read Scala SortedMap max() method with example The max() method is utilized to find the largest element of the SortedMap. Method Definition: def max: (A, B) Return Type: It returns the largest element of the SortedMap. Example #1: Scala // Scala program of max() // method import scala.collection.immutable.SortedMap // Creating object object GfG 1 min read Like