Apr
12
how to convert an Array into a Map in Scala
Hey there! Welcome to ClearUrDoubt.com. In this post, we will look at a Scala program to convert an Array into an instance of Map. Let’s look at the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
object ArrayToMap extends App { val ints = Array(1, 2, 3, 4) val strings = Array("ONE", "TWO", "THREE", "FOUR") //Convert above ints and strings arrays into a Map val mapIntToStrings = (ints zip strings).toMap println("Int to String Map = " + mapIntToStrings.mkString(", ")) //Convert above ints and corresponding indices into a Map val mapIntsToIndices = (ints zipWithIndex).toMap println("Int to Index Map = " + mapIntsToIndices.mkString(", ")) } |
Output: Happy learning. Please leave a reply in case of any Read more