I’ve recently started working with scala. I found it to be simple to start with. Scala offers Map Concept ,Maps allows us to store a collection of key-value pairs and to access the values by the keys associated with them,Scala provides mutable and immutable versions of map.There is a base map trait in package scala.collection ,and there is also two sub maps in packages scala.collection.mutable and scala.collection.immutable.
Lets us look at a quick example
scala> val worldMap=Map("Asia"->"India","Europe"->"France")
worldMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(Asia -> India, Europe -> France)
scala> print(worldMap)
Map(Asia -> India, Europe -> France)
In this example, Scala declares val as worldMap type (String, String). It is actually a Map[String, String] type.
By default it is immutable type of Map.If you prefer to use mutable Map then you have to import scala.collection.mutable.Map . For example,
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val worldMap=Map("Asia"->"India")
worldMap: scala.collection.mutable.Map[java.lang.String,java.lang.String] = Map(Asia -> India)
scala> worldMap +=("Africa"->"Kenya")
res2: worldMap.type = Map(Asia -> India, Africa -> Kenya)
In above example, you can see ,initially worldMap contains single key-value pair ,then after we add another key-value pair in worldMap. This is possible because worldMap type is mutable Map. if we can try this with the immutable Map then error would be appeared, For example,
scala> import scala.collection.immutable.Map
import scala.collection.immutable.Map
scala> val worldMap=Map("Asia"->"India")
worldMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(Asia -> India)
scala> worldMap +=("Africa"->"Kenya")
:12: error: reassignment to val
worldMap +=("Africa"->"Kenya")
Here reassignment error is occurred due to immutable Map type.
Accessing Map values :
In Scala, the analogy between functions and maps is particularly close because you use the () notation to look up key values. For example,
scala> val country=worldMap("Asia")
country: java.lang.String = India
scala> val country=worldMap("Africa")
country: java.lang.String = Kenya
If the map doesn’t contain a value for the requested key, an exception is thrown.To check if there is a key with the given value, call the contains method,For example,
scala> val country=worldMap("Europe")
java.util.NoSuchElementException: key not found: Europe
scala> val country=if(worldMap.contains("Europe")) worldMap("Europe") else 0
country: Any = 0
scala> val country=if(worldMap.contains("Asia")) worldMap("Asia") else 0
country: Any = India
Filter Map by Key Set
To filter a Map keeping only the entries where the key is contained in a given Set
scala> val worldMap=Map("Asia"->"India","Europe"->"France")
worldMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Ma
p(Asia -> India, Europe -> France)
scala> worldMap.filterKeys(Set("Asia"))
res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(As
ia -> India)
Updating Map Values :
In a mutable map, you can update a map value, or add a new one,For example,
scala> worldMap("Asia")="Japan"
scala> print(worldMap)
Map(Asia -> Japan, Africa -> Kenya)
scala> worldMap("Africa")="Mali"
scala> print(worldMap)
Map(Asia -> Japan, Africa -> Mali)
scala> worldMap +=("Europe"->"France")
res10: worldMap.type = Map(Europe -> France, Asia -> India, Africa -> Mali)
Iterating Over Map with Scala
Scala provides several different ways to iterate over a Map, below are some useful examples:
<strong>Example 1:</strong>
scala> val worldMap=Map("Asia"->"India","Europe"->"France")
worldMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(Asia -> India, Europe -> France)
scala> worldMap.foreach((w)=>println("Continent:" + w._1 + " Country :" + w._2))
Continent:Asia Country :India
Continent:Europe Country :France
In this example, the _1 maps to the key and the _2 maps to the value of the element w in the current iteration.
Example 2: Using a for-comprehension loop iterating Map
scala> val worldMap=Map("Asia"->"India","Europe"->"France")
worldMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Ma
p(Asia -> India, Europe -> France)
scala> for ((key, value) <- worldMap) println("Continent: " + key + " Country: "
+ value)
Continent: Asia Country: India
Continent: Europe Country: France