Map

Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys are always unique in the Map, but values need not be unique. Key-Values pairs can be any data type.Maps are also called Hash tables.

There are two kinds of Maps
  • immutable
  • 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.
 
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.

val m1=Map(key_1 -> value_1, key_2 -> value_2, key_3 -> value_3, ....) --Immutable

val m2 = scala.collection.mutable.Map(key_1 -> value_1,key_2 -> value_2, key_3 -> value_3, ....) -- Mutable
 
Operations on a Scala Maps
There are three basic operations we can carry out on a Map:
  • 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. 
Creating a Map with Initial elements
object demo {
def main(args : Array[String]){
val name = Map("Jhon" -> 30,"Tom" -> 20,"Charlie" -> 50)
println("print the elements of the Map")
println(name)
println("print the specific key value")
println(name("Jhon"))
println(name.contains("Jhon"))
}
}
print the elements of the Map
Map(Jhon -> 30, Tom -> 20, Charlie -> 50)
print the sepecfic key value
30
true
Creating a Empty Map with Initial elements
object demo {
def main(args : Array[String]){
val name = Map()
println("print the elements of the Map")
println(name)
}
}
Create Maps with pair Syntax
object demo {
def main(args: Array[String]) {
val colors = Map(("bird", "blue"), ("fox", "red"))
val result1 = colors("bird")
println(result1)
val result2 = colors("fox")
println(result2)
}
}
Create a mutable Map
object test {
def main(args: Array[String]): Unit = {
val Name = scala.collection.mutable.Map("Jhon" -> 30, "Tom" -> 20, "Charlie" -> 50)
println("print the elements of the Map")
println(Name)
println("updating the value of the map")
Name("Jhon") = 32
println(Name)
}
}
Add and remove elements in MAP
object test {
def main(args : Array[String]){
val Name = scala.collection.mutable.Map("Jhon" -> 30,"Tom" -> 20,"Charlie" -> 50)
println("print the elements of the Map")
println(Name)
println("After adding key and value to the map")
Name += ("Ajay" -> 10, "Dinesh" -> 60) // Add elements to the map
println(Name)
println("remove key and value from map") // Remove elements to the map
Name -= ("Ajay" )
println(Name)
}
}
Concatenation the elements in MAP 
object test {
def main(args: Array[String]) {
val stud1 = Map(12 -> "Reena", 13 -> "Micheal" , 14 -> "Peter")
val stud2 = Map(15 -> "Russel", 16 -> "Mark" , 17 -> "Steve")
var student = stud1 ++ stud2
println( "stud1 ++ stud2 : " + student )
val stu = stud1.++(stud2)
println( "stud1.++(stud2)) : " + stu )
}
}

Comparing two elements in a MAP  
object demo
{
def main(args :Array[String])
{
val stud1 = Map(12 -> "Reena", 13 -> "Micheal" , 14 -> "Peter")
val stud2 = Map(15 -> "Russel", 16 -> "Mark" , 17 -> "Steve")
val stud3 = Map(12 -> "Reena", 13 -> "Micheal" , 14 -> "Peter")

val result=stud1.equals(stud3)
println(result)
}
}
Map Using foreach
object Demo
{
def main(args:Array[String])
{
val m1 = Map(3 -> "geeks", 1 -> "for", 2 -> "cs", 6 -> "geeks")
val result = m1.foreach(x => println("key=" + x._1 + ", value=" + x._2))
}
}



No comments:

Post a Comment