Guozhang Wang created KAFKA-1610:
------------------------------------

             Summary: map v.s. mapValues in Scala
                 Key: KAFKA-1610
                 URL: https://issues.apache.org/jira/browse/KAFKA-1610
             Project: Kafka
          Issue Type: Bug
            Reporter: Guozhang Wang
             Fix For: 0.9.0


In our current Scala code base we have 40+ usages of mapValues, however it has 
an important semantic difference with map, which is that "map" creates a new 
map collection instance, while "mapValues" just create a map view of the 
original map, and hence any further value changes to the view will be 
effectively lost.

Example code:

{code}
scala> case class Test(i: Int, var j: Int) {}
defined class Test

scala> val a = collection.mutable.Map(1 -> 1)
a: scala.collection.mutable.Map[Int,Int] = Map(1 -> 1)

scala> val b = a.mapValues(v => Test(v, v))
b: scala.collection.Map[Int,Test] = Map(1 -> Test(1,1))

scala> val c = a.map(v => v._1 -> Test(v._2, v._2))
c: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,1))

scala> b.foreach(kv => kv._2.j = kv._2.j + 1)

scala> b
res1: scala.collection.Map[Int,Test] = Map(1 -> Test(1,1))

scala> c.foreach(kv => kv._2.j = kv._2.j + 1)

scala> c
res3: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,2))

scala> a.put(1,3)
res4: Option[Int] = Some(1)

scala> b
res5: scala.collection.Map[Int,Test] = Map(1 -> Test(3,3))

scala> c
res6: scala.collection.mutable.Map[Int,Test] = Map(1 -> Test(1,2))
{code}

We need to go through all these mapValue to see if they should be changed to map



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to