Map is a collection of <key, value> pairs.
Syntax
var/val variableName = Map(key1 -> val1, key2 -> val2 ……keyN -> valN)
Example
val employeeNamesById = Map(1 -> "Ram", 2 -> "Gopi", 3 -> "Joel")
scala> val employeeNamesById = Map(1 -> "Ram", 2 -> "Gopi", 3 -> "Joel")
val employeeNamesById: scala.collection.immutable.Map[Int,String] = Map(1 -> Ram, 2 -> Gopi, 3 -> Joel)
As you see the output, Scala defines an Immutable map by default. If you want a mutable map, you should define it explicitly.
How to access the elements of Map using a key?
Syntax
Map(key)
Example
employeeNamesById(1)
scala> employeeNamesById(1)
val res180: String = Ram
scala> employeeNamesById(2)
val res181: String = Gopi
How to define a mutable map?
You can define a mutable map using ‘scala.collection.mutable.Map’ class.
scala> val employeeMutableMap = scala.collection.mutable.Map(1 -> "Ram", 2 -> "Gopi", 3 -> "Joel")
val employeeMutableMap: scala.collection.mutable.Map[Int,String] = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel)
When you try to access a key that is not in the map, then Scala throws NoSuchElementException.
scala> employeeMutableMap(12)
java.util.NoSuchElementException: key not found: 12
at scala.collection.MapOps.default(Map.scala:251)
at scala.collection.MapOps.default$(Map.scala:250)
at scala.collection.AbstractMap.default(Map.scala:381)
at scala.collection.mutable.HashMap.apply(HashMap.scala:417)
... 32 elided
If you want to avoid this exception, use getOrElse method. This method returns the value associated with the key if key exist, else it returns the specified value.
scala> employeeMutableMap.getOrElse(12, "no_name")
val res185: String = no_name
How to add new key-value pairs to the map?
If a map is mutable, you can use the () operator to add new key-value pairs.
Example
employeeMutableMap(4) = "Sailu"
scala> employeeMutableMap
val res186: scala.collection.mutable.Map[Int,String] = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel)
scala> employeeMutableMap(4) = "Sailu"
scala> employeeMutableMap(5) = "Venkat"
scala> employeeMutableMap
val res189: scala.collection.mutable.Map[Int,String] = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 4 -> Sailu, 5 -> Venkat)
How to update the value associated with the given key?
If a map is mutable, you can use () operator to update the existing value associated with the key.
Example
employeeMutableMap(4) = "Rahim"
scala> employeeMutableMap(4) = "Rahim"
scala> employeeMutableMap
val res191: scala.collection.mutable.Map[Int,String] = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 4 -> Rahim, 5 -> Venkat)
How to remove an element from Map?
You can use -= operator to remove an element from the map.
Example
employeeMutableMap -= 4
Above statement remove a key->value association from the map where the key is 4.
scala> println(employeeMutableMap)
HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 4 -> Rahim, 5 -> Venkat)
scala> employeeMutableMap -= 4
val res194: employeeMutableMap.type = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 5 -> Venkat)
scala> println(employeeMutableMap)
HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 5 -> Venkat)
Similarly, you can use the += operator to add new key->value pairs to the map.
scala> employeeMutableMap += (6 -> "Siva")
val res196: employeeMutableMap.type = HashMap(1 -> Ram, 2 -> Gopi, 3 -> Joel, 5 -> Venkat, 6 -> Siva)
Whenever you perform CRUD on Immutable Map, Scala return new Instance of Immutable map with the updated content
scala> countryCapitals
val res204: scala.collection.immutable.Map[String,String] = HashMap(AFGHANISTAN -> KABUL, BRAZIL -> BRASILIA, CHILE -> SANTIAGO, INDIA -> NEW DELHI, ANGOLA -> LUANDA)
scala> var countryCapitals1 = countryCapitals - "BRAZIL"
var countryCapitals1: scala.collection.immutable.Map[String,String] = HashMap(AFGHANISTAN -> KABUL, CHILE -> SANTIAGO, INDIA -> NEW DELHI, ANGOLA -> LUANDA)
scala> countryCapitals
val res205: scala.collection.immutable.Map[String,String] = HashMap(AFGHANISTAN -> KABUL, BRAZIL -> BRASILIA, CHILE -> SANTIAGO, INDIA -> NEW DELHI, ANGOLA -> LUANDA)
As you see above snippet, countryCapitals - "BRAZIL" do not delete the association BRAZIL -> BRASILIA in the original map.
No comments:
Post a Comment