I am new to Scala and trying out the map function on a Map. Here is my Map:
scala> val map1 = Map ("abc" -> 1, "efg" -> 2, "hij" -> 3)
map1: scala.collection.immutable.Map[String,Int] =
Map(abc -> 1, efg -> 2, hij -> 3)
Here is a map function and the result:
scala> val result1 = map1.map(kv => (kv._1.toUpperCase, kv._2))
result1: scala.collection.immutable.Map[String,Int] =
Map(ABC -> 1, EFG -> 2, HIJ -> 3)
Here is another map function and the result:
scala> val result1 = map1.map(kv => (kv._1.length, kv._2))
result1: scala.collection.immutable.Map[Int,Int] = Map(3 -> 3)
The first map function returns all the members as expected however the second map function returns only the last member of the Map. Can someone explain why this is happening?
Thanks in advance!
In Scala, a Map
cannot have duplicate keys. When you add a new key -> value
pair to a Map
, if that key already exists, you overwrite the previous value. If you're creating maps from functional operations on collections, then you're going to end up with the value corresponding to the last instance of each unique key. In the example you wrote, each string key of the original map map1
has the same length, and so all your string keys produce the same integer key 3
for result1
. What's happening under the hood to calculate result1
is:
"abc" -> 1
to 3 -> 3
and add it to the map. Result now contains 1 -> 3
."efg" -> 2
to 3 -> 2
and add it to the map. Since the key is the same, you overwrite the existing value for key = 3
. Result now contains 2 -> 3
. "hij" -> 3
to 3 -> 3
and add it to the map. Since the key is the same, you overwrite the existing value for key = 3
. Result now contains 3 -> 3
. Map(3 -> 3
)`.Note: I made a simplifying assumption that the order of the elements in the map iterator is the same as the order you wrote in the declaration. The order is determined by hash bin and will probably not match the order you added elements, so don't build anything that relies on this assumption.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With