Scala - Map class (Mapping month numbers to names and vice versa) Code Example

The code for Map class (Mapping month numbers to names and vice versa)

object MyClass {
    def main(args: Array[String]) {
        val months_Num2Name = Map(
          1  -> "Jan",
          2  -> "Feb",
          3  -> "Mar",
          4  -> "Apr",
          5  -> "May",
          6  -> "Jun",
          7  -> "Jul",
          8  -> "Aug",
          9  -> "Sep",
          10 -> "Oct",
          11 -> "Nov",
          12 -> "Dec"
        )
        
        val months_Name2Num = Map(
          "Jan" -> 1,
          "Feb" -> 2,
          "Mar" -> 3,
          "Apr" -> 4,
          "May" -> 5,
          "Jun" -> 6,
          "Jul" -> 7,
          "Aug" -> 8,
          "Sep" -> 9,
          "Oct" -> 10,
          "Nov" -> 11,
          "Dec" -> 12
        )
        
        println(months_Num2Name(8))
        println(months_Name2Num("Dec"))
    }
}

/*
Output:
Aug
12
*/
Code by IncludeHelp, on August 15, 2022 22:58

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.