Map is a collection of key-value pairs. In other words, it is similar to dictionary. Keys are always unique while values need not be unique. Key-value pairs can have any data type. However, data type once used for any key and value must be consistent throughout. Maps are classified into two types: mutable and immutable. By default Scala uses immutable Map. In order to use mutable Map, we must import scala.collection.mutable.Map class explicitly.
How to create Scala Maps
Maps can be created in different ways based upon our requirement and nature of the Map. We have different syntax depending upon whether the Map is mutable or immutable.
Syntax :
Operations on a Scala Map
There are three basic operations we can carry out on a Map:
// Immutable variable = Map(key_1 -> value_1, key_2 -> value_2, key_3 -> value_3, ....) // Mutable variable = scala.collection.mutable.Map(key_1 -> value_1, key_2 -> value_2, key_3 -> value_3, ....)
- keys: In Scala Map, This method returns an iterable containing each key in the map.
- values: Value method returns an iterable containing each value in the Scala map.
- isEmpty: This Scala map method returns true if the map is empty otherwise this returns false.
// Scala map program of
// Accessing Values Using Keys
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapIm = Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
// Accessing score of Ajay
val ajay = mapIm("Ajay")
println(ajay)
}
}
30If we try to access value associated with the key "John", we will get an error because no such key is present in the Map. Therefore, it is recommended to use contains() function while accessing any value using key. This function checks for the key in the Map. If the key is present then it returns true, false otherwise.
// Scala map program of
// Accessing Values Using
// Keys by contains() function
// Creating object
object GFG
{
// Main methode
def main(args:Array[String])
{
val mapIm = Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
// the key check in the Map
val ajay = if(mapIm.contains("Ajay"))
mapIm("Ajay") else 0
val john = if(mapIm.contains("John"))
mapIm("John") else 0
println("Ajay:" + ajay)
println("John:" + john)
}
}
Ajay:30 John:0
// Scala map program of
// Updating the values
// in immutable map
// Creating an object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapIm = Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
println(mapIm)
//Updating
mapIm("Ajay") = 10
println(mapIm)
}
}
error: value update is not a member of scala.collection.immutable.Map[String, Int]Updating mutable Map:
// Scala map program of
// Updating the values
// in mutable map
// Creating Object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapMut = scala.collection.mutable.Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
println("Before Updating: " + mapMut)
// Updating
mapMut("Ajay") = 10
println("After Updating: " + mapMut)
}
}
Before Updating: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20) After Updating: Map(Ajay -> 10, Charlie -> 50, Bhavesh -> 20)
// Scala map program of
// Adding new key-value pair
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapMut = scala.collection.mutable.Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
println("Before Adding: "+mapMut)
// Adding a new key "Dinesh" and
// updating an existing key "Ajay"
mapMut += ("Ajay" -> 10, "Dinesh" -> 60)
println("After Adding: "+mapMut)
}
}
Before Adding: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20) After Adding: Map(Ajay -> 10, Dinesh -> 60, Charlie -> 50, Bhavesh -> 20)
// Scala map program of
// Deleting new key-value pair
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapMut = scala.collection.mutable.Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
println("Before Deleting: "+mapMut)
// Deleting key-value pairs with
// keys "Ajay" and "Charlie"
mapMut -= ("Ajay", "Charlie")
println("After Deleting: " + mapMut)
}
}
Before Deleting: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20) After Deleting: Map(Bhavesh -> 20)
// Scala map program of
// Iteration in a Map
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
val mapMut = scala.collection.mutable.Map("Ajay" -> 30,
"Bhavesh" -> 20,
"Charlie" -> 50)
// (k, v) is a tuple with two elements
for((k, v) <- mapMut)
{
//where k is key and v is value
print("Key:"+k+", ")
println("Value:"+v)
}
}
}
Key:Ajay, Value:30 Key:Charlie, Value:50 Key:Bhavesh, Value:20
// Scala map program of
// Empty Map
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
// Creation of Map having key-value
// pairs of type (String, Int)
val mapMut = scala.collection.mutable.Map[String, Int]()
println("Empty Map: " + mapMut)
// Adding new entry
mapMut += ("Charlie" -> 50)
println("New Entry: " + mapMut)
}
}
Empty Map: Map() New Entry: Map(Charlie -> 50)