When concatenating two immutable maps, it seems that the elements of the right operand will "overwrite" the elements of the left one:
scala> List((1, 2), (5, 6)).toMap ++ List((5, 9)).toMap res13: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 5 -> 9) scala> List((5, 9)).toMap ++ List((1, 2), (5, 6)).toMap res14: scala.collection.immutable.Map[Int,Int] = Map(5 -> 6, 1 -> 2)
I would like to know, if this is a rule in Scala ?
From the Scala API I could not figure out this question.
The concatenation of Scala map is obtained by utilizing ++ operator. Return Type: It returns a single map by concatenating two maps but it separates the identical keys. As we can see in above example, we joined two maps by using ++ operator.
If you want to use the mutable Map, you'll have to import scala. collection. mutable. Map class explicitly.
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.
A Map is an Iterable consisting of pairs of keys and values (also named mappings or associations). Scala's Predef object offers an implicit conversion that lets you write key -> value as an alternate syntax for the pair (key, value) .
Yes, this behaviour is constant
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