Regarding Daniel's answer, but with a few optimizations and my formatting preferences:
val myMap = Map("1" -> "2", "3"->"4") val s = myMap.view map { case (key, value) => "Key: " + key + "\nValue: " + value } mkString ("", "\n", "\n")
Optimization:
- By first creating a
view on a map, I avoid creating an intermediate collection - When profiling, direct string concatenation is faster than
String.format
Kevin wright
source share