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:
import scala.collection.mutable.SortedSet
object GfG
{
def main(args : Array[String])
{
val s 1 = SortedSet( 13 , 22 , 19 , 21 )
s 1 .foreach(x => println(x))
}
}
|
Example #2:
import scala.collection.mutable.SortedSet
object GfG
{
def main(args : Array[String])
{
val s 1 = SortedSet( 5 , 7 , 8 , 3 , 2 )
s 1 .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