I have gone through couple of blog and got to know that scala Map can not have duplicate key. But when I tried then it works , I mean didn't throw any error. So is it true or ?
val subjects = Map(1 -> "DS",2 -> "C++", 3 -> "Java", 1-> "DF")
println(subjects)
output : Map(1 -> DF, 2 -> C++, 3 -> Java)
It is not showing in output but ideally it should throw error while running command?
In Scala, a Map is a collection of key-value pairs where each key is unique. A Map cannot have a duplicate key because the key declaration follows the hashing technique.
If you try to declare the same key with multiple values, particularly in the scala Map case it updates the value of that particular key and won't throw any exception.
Eg:
val map = Map(1 -> "london", 2 -> "world", 1 -> "hello")
if you print this, the output must be Map(1 -> "hello",2 -> "world")
val map = Map(1 -> "hello", 2 -> "world", 1 -> "london")
if you print this, the output must be Map(1 -> "london",2 -> "world")