Thursday, 10 September 2020

Scala: Iterate over a map

Syntax

for((key, value) <- map){

 

}

 

Example

scala> val employeeNamesById = Map(1 -> "Ram", 2 -> "Gopi", 3 -> "Joel")
val employeeNamesById: scala.collection.immutable.Map[Int,String] = Map(1 -> Ram, 2 -> Gopi, 3 -> Joel)

scala> for((id, name) <- employeeNamesById){
     |   println(s"$id = $name")
     | }
1 = Ram
2 = Gopi
3 = Joel




Previous                                                    Next                                                    Home

No comments:

Post a Comment