Hi,
- You can either declare an empty Scala map or one with values. Here is an example:
scala> var m:Map[String,Int]=Map()
m: Map[String,Int] = Map()
Here, you must include the type annotation so it can assign proper types to the variables.
- Declaring a Scala Map with values
When you provide values, Scala will use those to infer the type of variables. So, we don’t need to include the type annotations.
Here is an example:
scala> var m=Map("Niti"->0,"Giti"->1)
m: scala.collection.immutable.Map[String,Int] = Map(Niti -> 0, Giti -> 1)
Now to add key-value pair you need to follow this:
scala> m+=("Ruchi"->2)
scala> m
res1: scala.collection.immutable.Map[String,Int] = Map(Niti -> 0, Giti -> 1, Ruchi -> 2)
If you want to know more about Apache Spark Scala, It's highly recommended to go for the Spark Certification Course today.