How's the following function to update an immutable Map's values?

scala> def updateMap[A](map: Map[A, Set[A]], item: A, toAdd: A) = { 
    map.map(x => if(x._1 == item) (x._1, (x._2 + toAdd)) else (x._1, x._2)) 

scala> val map = Map(1 -> Set(2), 3 -> Set(4))

scala> updateMap(map, 1, 5)
res1 ...= Map(1 -> Set(2, 5), 3 -> Set(4))

You iterate through the whole Map, which is O(n) and therefore slow as hell. Worse, you even recreate every element and put it into another element, which is even slower.

Why not just do:

scala> val map = Map(1 -> Set(5,3), 2 -> Set(7,3))
map: scala.collection.immutable.Map[Int,scala.collection.immutable.Set[Int]] = Map(1 -> Set(5, 3), 2 -> Set(7, 3))

scala> def updateMap[A, B](map: Map[A, Set[B]], key: A, value: B) =
         map + ((key, map.getOrElse(key, Set()) + value))
updateMap: [A, B](map: Map[A,Set[B]], key: A, value: B)scala.collection.immutable.Map[A,Set[B]]

scala> updateMap(map, 1, 9)
res0: scala.collection.immutable.Map[Int,Set[Int]] = Map(1 -> Set(5, 3, 9), 2 -> Set(7, 3))

And you probably want to introduce another type parameter B to the signature of updateMap to allow values of a different type than the one of the key.

  • \$\begingroup\$ Using an if/else with if (map.contains (key)) would work without O(n) but it's not a succinct. Nice code above, thanks. \$\endgroup\$ – Kevin Meredith Jun 27 '14 at 15:44
  • \$\begingroup\$ @Kevin I'm not sure if the original post was wrong, but the behavior is different in this answer. In the original post, if the key did not already exist in the map, the update did nothing. \$\endgroup\$ – toto2 Jun 27 '14 at 19:55
  • \$\begingroup\$ And what if I want to update the map if the value is not empty only? \$\endgroup\$ – Alexandr Dec 13 '16 at 17:15

Pattern matching is a more readable and important feature of Scala, which you can use.

And in the else condition, you are using (x._1, x._2); you can use x in place of it.

  def updateMap[A](map: Map[A, Set[A]], item: A, toAdd: A) = { 
    map.map(x => (x._1 == item) match{ 
      case true=>(x._1, (x._2 + toAdd)) 
      case false=>x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.