Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Scala program to add or update entries in a Map.
we can use “+” method on the map instance to add/update elements. If called this method with an existing key, the corresponding value gets updated. If called with new key, the entry gets added to the Map.
Let’s look at a simple program for demonstration:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.clearurdoubt.maps object MapDemo extends App { val map = Map[Int, String](1 -> "ONE", 2 -> "TWO", 3 -> "TWO") println("Original map: " + map) println() // Update an existing entry in a map or add a new entry val correctedMap = map + (3 -> "Three") + (4 -> "FOUR") println("After update: " + correctedMap) } |
Output:
Happy learning.
Please leave a reply in case of any queries.