Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate two immutable maps - which elements are preferred?

Tags:

map

scala

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.

like image 557
John Threepwood Avatar asked Jul 31 '12 14:07

John Threepwood


People also ask

What are two ways to concatenate two maps?

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.

How to use mutable Map in scala?

If you want to use the mutable Map, you'll have to import scala. collection. mutable. Map class explicitly.

Are Maps mutable?

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.

How to define a Map in 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) .


1 Answers

Yes, this behaviour is constant

like image 178
Nikita Volkov Avatar answered Oct 06 '22 23:10

Nikita Volkov