Map
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. Map is a collection of key and value pairs which are stored internally using a Hash Table data structure. In order to use
mutable Map, we must import scala.collection.mutable.Map class
explicitly.
Syntax :
// Immutable
var test:Map[Int,String] = Map(key_1 -> value_1, key_2 -> value_2,
key_3 -> value_3, ....)
// Mutable
var test:Map[Int,String] = scala.collection.mutable.Map(key_1 ->
value_1, key_2 -> value_2, ....)
//Values can be accessed using Map variable name and key.
object SMAP {
def main(args:Array[String])
{
var mapIn = Map("Ajay" -> 30,
"Bhavesh" -> 20, "Charlie" -> 50)
val ajay = mapIn("Ajay")
println(ajay)
val ajayVal = if(mapIn.contains("Ajay"))
mapIn("Ajay") else 0
val johnVal = if(mapIn.contains("John"))
mapIn("John") else 0
println("Ajay : "+ajayVal)
println("John : "+johnVal)
//println(mapIn(20))
}
}
Output :
Ajay : 30 John : 0
Methods use in Map
//Updating Map value
val mapIMut =
scala.collection.mutable.Map("Ajay" -> 30, "Bhavesh"
-> 20,"Charlie" -> 50)
println("Before Updating: " + mapIMut)
//Output
Before Updating: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
// Updating mapIMut("Ajay") = 10
println("After Updating: " + mapIMut)
println("Before Updating: " + mapIMut)
//Output
Before Updating: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
// Updating mapIMut("Ajay") = 10
println("After Updating: " + mapIMut)
//Output
After Updating: Map(Ajay -> 10, Charlie -> 50, Bhavesh -> 20)
After Updating: Map(Ajay -> 10, Charlie -> 50, Bhavesh -> 20)
//Adding Map Value
val mapNKVPMut =
scala.collection.mutable.Map("Ajay" -> 30,"Bhavesh"
-> 20,"Charlie" -> 50)
println("Before Adding: "+mapNKVPMut)
println("Before Adding: "+mapNKVPMut)
//Output
Before Adding: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
Before Adding: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
// Adding a new key "Dinesh" and updating
an existing key "Ajay"
mapNKVPMut += ("Ajay" -> 10,
"Dinesh" -> 60)
println("After Adding: "+mapNKVPMut)
//Output
//Output
After Adding: Map(Ajay -> 10, Dinesh -> 60,
Charlie -> 50, Bhavesh -> 20)
//Deleting Map Value
val mapDKVPMut =
scala.collection.mutable.Map("Ajay" -> 30,"Bhavesh"
-> 20,"Charlie" -> 50)
println("Before Deletion: "+mapDKVPMut)
println("Before Deletion: "+mapDKVPMut)
//Output
Before Deletion: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
Before Deletion: Map(Ajay -> 30, Charlie -> 50, Bhavesh -> 20)
mapDKVPMut -= ("Ajay")
println("After Deletion : "+mapDKVPMut)
//Output
println("After Deletion : "+mapDKVPMut)
//Output
After Deletion : Map(Charlie -> 50, Bhavesh
-> 20)
//Iteration in Map
val mapItMut =
scala.collection.mutable.Map("Ajay" -> 30,"Bhavesh"
-> 20,"Charlie" -> 50)
// (k, v) is a tuple with two elements
for((k, v) <- mapItMut)
{
//where k is key and v is value
print("Key:"+k+", ")
println("Value:"+v) }
Output:
Key:Ajay, Value:30
Key:Charlie, Value:50
Key:Bhavesh, Value:20
//Size of Map
prinltn(mapItMut.size)
//3
//Use of get function
var first:Map[Int,String] = Map[Int,String]() // Empty Map //
Map[key,Value]
first +=(1->"aakash") // u are adding the value
inside the map
first +=(2->"kumar") // u are adding the value
inside the map
first +=(3->"umesh")
first +=(4->"manii")
first.get(3).get.length // there is a function which is called as
get, if u pass the key inside the get function , it will give u the value ,
Syntax :=> get(key)
//Get all keys from a Map
first.keys
Set(1, 2, 3, 4)
//Get all values from a Map
first.values
MapLike.DefaultValuesIterable(aakash, kumar, umesh, manii)
//use of get function
println(first.get(1))
Some(aakash) //result is wrapped in Some function
//Now we have to use get function one more time to get value of key 1.
println(first.get(3).get)
//aakash
//use of contains function
first.contains(20) // it will Check the whether key is present or not ,
if present then true else false
//Use of take function
println(first.take(2))
//Output is Map(1 -> aakash, 2 -> kumar)
//Use of takeRight function
println(first.takeRight(2))
//Output is Map(3 -> umesh, 4 -> manii)
//Use of drop function
println(first.drop(2))
//Output is Map(3 -> umesh, 4 -> manii)
//Use of dropRight function
println(first.dropRight(2))
//Output is Map(1 -> aakash, 2 -> kumar)
//Some sample code :
var first:Map[Int,String] = Map[Int,String]() // Empty Map
// Map[key,Value]
first +=(1->"aakash") // u are adding the value
inside the map
first +=(2->"kumar") // u are adding the
value inside the map
first +=(3->"umesh")
first +=(4->"manii")
for((k,v)<-first)
{
if(v.contains("a") &&
(v.contains("r")))
{
println("key is " + k)
}
}
//Output: key is 2
//Other example
var test_map : Map[Int,Array[Int]] = Map[Int,Array[Int]]() // key
is Integer and Value is Array of integer
test_map += (1->Array(1,2))
test_map += (2->Array(3,4,5,6))
var res = test_map.get(2).get.length // return 4 as output
//Other example
var test_map12 : Map[Int,Map[String,Double]] = Map[Int,Map[String,Double]]()
// key is Integer and Value is Again a Map Where key is String and value is
double
test_map12 += (1->Map("aakash"->9.5))
test_map12 += (2->Map("kumar"->10.5))
//test_map12
println(test_map12.get(2).get.get("kumar").get)
//Output:
10.5
var first:Map[Int,String] = Map[Int,String]() // Empty Map // Map[key,Value]
first +=(1->"aakash") // u are adding the value
inside the map
first +=(2->"kumar") // u are adding the value
inside the map
first +=(3->"umesh")
first +=(4->"manii")
// multiple key declaration happening
first -= (1,3)
//Delete key value pair of key 1 & 3.
No comments:
Post a Comment