How to convert Java foreach loop in a Scala equivalent?
java, scala, scala-java-interop
Solution
The problem is that you use incorrect syntax. This
for (allEntriesMap.entrySet[String, List[String]] allEntriesMapEntry : allEntriesMap.entrySet()) {
}
Should be written as:
for (entry: Map.Entry[String, List[String]] <- allEntriesMap.entrySet()) {
}
or simply
for (entry <- allEntriesMap.entrySet) {
}
Moreover, if you're using java collections type you have to import `scala.collections.JavaConversions._` into scope (that will implicitly convert java collections into scala ones, so you may use all set of operations on them).
Problem
I'm trying to convert below Java code to Scala: ``` Map<String, List<String>> allEntriesMap = getEntries(); for (Map.Entry<String, List<String>> allEntriesMapEntry : allEntriesMap .entrySet()) { } ``` Here is the current Scala version of above Java code: ``` var allEntriesMap : Map[String, List[String]] = getEntries(); for (allEntriesMap.entrySet[String, List[String]] allEntriesMapEntry : allEntriesMap.entrySet()) { } ``` I'm receiving this error for line ``` for (allEntriesMap.entrySet[String, List[String]] allEntriesMapEntry : ``` illegal start of simple pattern How can above code be finished so that it performs same Java functionality but written in Scala?