Updated April 11, 2023
Introduction to Scala Map Foreach
Scala Map is also known as HASH TABLE is a collection of Key/Value pairs called Hash Tables. Key is used to access the values associated with it. Values in a Map can be same but keys are always unique. Map is a higher-order function, having for Each method that takes some function as parameter and applies the function to every element of the source collection.
Scala Map has FOR EACH function which is used to iterate and apply function to all elements of the Scala MAP. It returns all the elements in a MAP by iterating and applying the passed function to each of them. It is also used for pattern matching in Scala. A match expression with Scala For Each is a very convenient and readable approach in Scala. We can use the tuple to access the key and value of a Scala Map. For Each method can also be used to iterate over the Key or Simply the value from the Map.
Syntax:
The syntax for Scala Map Foreach function is:
def foreach(f: ((a, b)) => Unit): Unit
It returns all the element of the Map applying the function to them.
Snapshot:
Working of Map Foreach Function
Let us see how the For Each function works in Scala.
The Scala For Each function is used to iterate over a Scala MAP. Starting with the for keyword it is used for iteration of Scala Map. The for-each loop usually maintain no Explicit counter i.e it is applied to all the Key value pair in a Scala MAP.
For each method is easier to code and syntactically more code friendly.
Each method takes a procedure, yielding a return type Unit. The procedure is applied to all the elements having unit as the result.
This also makes the functionality of a For Each where it is easier for a user to code and print the output needed.
Example of Scala Map Foreach
There is lots of usage of FOR Each Method in Scala Map we will check some of its functionality and advantage with the help of examples.
Example #1: Create a Scala MAP.
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "Demo", 4 -> "Code")
Map is Created.
val r = map1.foreach(r => println("key=" + r._1 + ", value=" + r._2))
Iterate each value of a Map using the Scala
Here we are using each method to iterate the map and get the key and value.
Full Code:
object Demo {
def main(args: Array[String]) {
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "This", 4 -> "Code")
val r = map1.foreach(r => println("key=" + r._1 + ", value=" + r._2))
}
}
The key and value are iterated using the Scala Map.
Screenshot:
If the same value is taken twice, the identical elements are taken only once.
Let’s check this with an Example adding a similar key to the above Map.
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "This", 4 -> "Code" , 3 -> "This")
Map created with duplicate value.
Let iterate it with for each loop .
val r = map1.foreach(r => println("key=" + r._1 + ", value=" + r._2))
The output will not print the identical object.
Full Code:
object Demo {
def main(args: Array[String]) {
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "This", 4 -> "Code" , 3 -> "This")
val r = map1.foreach(r => println("key=" + r._1 + ", value=" + r._2))
}
}
Output:
Let iterate the Key and value explicitly over the same Map.
Code:
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "This", 4 -> "Code")
val a = map1.keys.foreach( key => println(key + " : "+map1.get(key)))
This will print all the key in the Scala Map.
val b = map1.values.foreach( value => println(value + " : "+value))
This will print all the values from the Scala Map.
Full Code:
object Demo {
def main(args: Array[String]) {
val map1 = Map(1 -> "This ", 2 -> "is", 3 -> "This", 4 -> "Code")
val a = map1.keys.foreach( key => println(key + " : "+map1.get(key)))
val b = map1.values.foreach( value => println(value + " : "+value))
}
}
Output:
From these various examples, we saw how we can implement for each loop in Scala Map.
Rules and Regulations for Map Foreach
Let us see the various Rules and Regulation for creating a For Each with Scala MAP.
- The For Each loop is used to iterate all the elements in a Map.
- We can apply function to each and every element using the Scala Map For Each.
- Scala Map key and Value pairs can be iterated using the for-each loop in Scala.
- We can use the match expression using for each method.
- For Each method is also used to iterate over tuples.
- The return type is Unit.
- The For Each method can be used to directly to print element in Scala.
- Modification is not advisable to do it with For Each.
- For Each loop doesn’t keeps the track of index.
- Used to iterate object in forward direction .
- Single Step Iteration is followed in Map For Each.
Conclusion
From the above article, we saw the use of FOR Each Method in Scala Map. From various example and classification, we tried to understand how the FOR EACH method works in Scala Map function and what are is use in the programming level.
We also saw the internal working and the advantages of having for each loop in Key-Value pair which we are defining for various programming purpose. Also, the syntax and examples helped us to understand much precisely the function.
Recommended Articles
We hope that this EDUCBA information on “Scala Map Foreach” was beneficial to you. You can view EDUCBA’s recommended articles for more information.