By Alvin Alexander. Last updated: June 4, 2016
Nothing too earth shattering here today, but if you need an example of the Scala Map class syntax (how to create a Scala Map), or just want to copy and paste a map of month names to numbers (or numbers to names), I hope the following code is helpful:
package scalamaps object MapOfMonths extends App { val monthNumberToName = Map( 1 -> "January", 2 -> "February", 3 -> "March", 4 -> "April", 5 -> "May", 6 -> "June", 7 -> "July", 8 -> "August", 9 -> "September", 10 -> "October", 11 -> "November", 12 -> "December" ) val monthNameToNumber = Map( "January" -> 1, "February" -> 2, "March" -> 3, "April" -> 4, "May" -> 5, "June" -> 6, "July" -> 7, "August" -> 8, "September" -> 9, "October" -> 10, "November" -> 11, "December" -> 12 ) println(monthNumberToName(4)) // "April" println(monthNameToNumber("July")) // 7 }
As you can see from the comments, the first print statement prints "April", and the second print statement prints the number 7.
Scala Map for the days of the week
What the heck, while I'm typing, here are some Scala maps for the days of the week:
val dayNameToNumber = Map( "Sunday" -> 1, "Monday" -> 2, "Tuesday" -> 3, "Wednesday" -> 4, "Thursday" -> 5, "Friday" -> 6, "Saturday" -> 7 ) val dayNumberToName = Map( 1 -> "Sunday", 2 -> "Monday", 3 -> "Tuesday", 4 -> "Wednesday", 5 -> "Thursday", 6 -> "Friday", 7 -> "Saturday" )
Here are some zero-based day of the week maps, if you prefer those:
val dayNameToNumber = Map( "Sunday" -> 0, "Monday" -> 1, "Tuesday" -> 2, "Wednesday" -> 3, "Thursday" -> 4, "Friday" -> 5, "Saturday" -> 6 ) val dayNumberToName = Map( 0 -> "Sunday", 1 -> "Monday", 2 -> "Tuesday", 3 -> "Wednesday", 4 -> "Thursday", 5 -> "Friday", 6 -> "Saturday" )
Again, I hope these Scala Map class examples are helpful.