Basic Operations On MAP: Example
Basic Operations On MAP: Example
Basic Operations On MAP: Example
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.
// Empty hash table whose keys are strings and values are integers:
var A:Map[Char,Int] = Map()
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)
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")
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.
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")
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)
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
Example
object Demo {
def main(args: Array[String]) {
val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" ->
"#CD853F")
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
Scala has a rich set of collection library. Collections are containers of things. Those containers
can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may
have an arbitrary number of elements or be bounded to zero or one element (e.g., Option).
Collections may be strict or lazy. Lazy collections have elements that may not consume memory
until they are accessed, like Ranges. Additionally, collections may be mutable (the contents of
the reference can change) or immutable (the thing that a reference refers to is never changed).
Note that immutable collections may contain mutable items.
For some problems, mutable collections work better, and for others, immutable collections work
better. When in doubt, it is better to start with an immutable collection and change it later if you
need mutable ones.
This chapter throws light on the most commonly used collection types and most frequently used
operations over those collections.