This topic is about Scala Collections – Map.
Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys are unique in the Map, but values need not be unique. Maps are also called Hash tables. There are two kinds of Maps, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can’t be changed.
By default, Scala uses the immutable Map. If you want to use the mutable Map, you’ll have to import scala.collection.mutable.Map class explicitly. If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map.
The Following is the example statements to declare immutable Maps −
// Empty hash table whose keys are strings and values are integers: var A:Map[Char,Int] = Map() // A map with keys and values. val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")
While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable. If we want to add a key-value pair to a Map, we can use the operator + as follows.
A + = ('I' -> 1) A + = ('J' -> 5) A + = ('K' -> 10) A + = ('L' -> 100)
Basic Operations on MAP
All operations on maps can be expressed in terms of the following three methods.
Sr.No | Methods & Description |
---|---|
1 | keysThis method returns an iterable containing each key in the map. |
2 | valuesThis method returns an iterable containing each value in the map. |
3 | isEmptyThis method returns true if the map is empty otherwise false. |
Try the following example program showing usage of the Map methods.
Example
object Demo { def main(args: Array[String]) { val colors = Map( "red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F" ) val nums: Map[Int, Int] = Map() println( "Keys in colors : " + colors.keys ) println( "Values in colors : " + colors.values ) println( "Check if colors is empty : " + colors.isEmpty ) println( "Check if nums is empty : " + nums.isEmpty ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
Keys in colors : Set(red, azure, peru) Values in colors : MapLike(#FF0000, #F0FFFF, #CD853F) Check if colors is empty : false Check if nums is empty : true
Concatenating Maps
You can use either ++ operator or Map.++() method to concatenate two or more Maps, but while adding Maps it will remove duplicate keys.
Try the following example program to concatenate two Maps.
Example
object Demo { def main(args: Array[String]) { val colors1 = Map( "red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F" ) val colors2 = Map( "blue" -> "#0033FF", "yellow" -> "#FFFF00", "red" -> "#FF0000" ) // use two or more Maps with ++ as operator var colors = colors1 ++ colors2 println( "colors1 ++ colors2 : " + colors ) // use two maps with ++ as method colors = colors1.++(colors2) println( "colors1.++(colors2)) : " + colors ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
colors1 ++ colors2 : Map(blue -> #0033FF, azure -> #F0FFFF, peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000) colors1.++(colors2)) : Map(blue -> #0033FF, azure -> #F0FFFF, peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000)
Print Keys and Values from a Map
You can iterate through the keys and values of a Map using “foreach” loop. Here, we used method foreach associated with iterator to walk through the keys. Following is the example program.
Example
object Demo { def main(args: Array[String]) { val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF","peru" -> "#CD853F") colors.keys.foreach{ i => print( "Key = " + i ) println(" Value = " + colors(i) ) } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
Key = red Value = #FF0000 Key = azure Value = #F0FFFF Key = peru Value = #CD853F
Check for a key in Map
You can use either Map.contains method to test if a given key exists in the map or not. Try the Following example program to key checking.
Example
object Demo { def main(args: Array[String]) { val colors = Map( "red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F" ) if( colors.contains( "red" )) { println("Red key exists with value :" + colors("red")) } else { println("Red key does not exist") } if( colors.contains( "maroon" )) { println("Maroon key exists with value :" + colors("maroon")) } else { println("Maroon key does not exist") } } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
Red key exists with value :#FF0000 Maroon key does not exist
In this topic we learned about Scala Collections – Map. To learn more, Click Here.