Scala Programming Language
  1. Scala Programming Language
  2. SI-5880

JavaConversions.MapWrapper generates wrong hashCode eventually

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.9.2
    • Fix Version/s: Scala 2.10.0-M3
    • Component/s: Collections
    • Labels:
      None
    • Environment:

      Darwin Kernel Version 11.4.0: Mon Apr 9 19:32:15 PDT 2012; root:xnu-1699.26.8~1/RELEASE_X86_64

      Description

      scala.collection.JavaConversions.MapWrapper's entrySet method seems to generate a Set having wrong hashCode() implementation. The hashcode has the possibility of conflict.

      I checked the problem with the following code:

      import scala.collection.JavaConversions._
      
      object App {
        def main(args:Array[String]) = {
          val jm:java.util.Map[String, String] = scala.collection.mutable.Map("a" -> "b", "b" -> "a")
          val es = jm.entrySet()
          val it = es.iterator
          println(it)
          while (it.hasNext) {
            val x = it.next()
            println("key=" + x.getKey + ", val=" + x.getValue)
            println("hashCode=" + x.##)
          }
        }
      }
      

      =========
      output
      =========

      scala.collection.JavaConversions$MapWrapper$$anon$1$$anon$5@6cb8
      key=a, val=b
      hashCode=195
      key=b, val=a
      hashCode=195
      
      1. App.scala
        0.6 kB
        Mitsunori Komatsu

        Activity

        Hide
        Mitsunori Komatsu added a comment -

        I think this issue is not "Blocker". But I couldn't change the priority...

        Show
        Mitsunori Komatsu added a comment - I think this issue is not "Blocker". But I couldn't change the priority...
        Hide
        Mitsunori Komatsu added a comment -

        I made the following patch. Thanks.

        $ diff -u JavaConversions.scala.orig JavaConversions.scala
        — JavaConversions.scala.orig 2012-03-19 01:09:26.000000000 +0900
        +++ JavaConversions.scala 2012-06-05 15:11:58.000000000 +0900
        @@ -703,7 +703,7 @@
        def getKey = k
        def getValue = v
        def setValue(v1 : B) = self.put(k, v1)

        • override def hashCode = k.hashCode + v.hashCode
          + override def hashCode = k.hashCode * 41 + v.hashCode
          override def equals(other: Any) = other match {
          case e : ju.Map.Entry[_, _] => k == e.getKey && v == e.getValue
          case _ => false
        Show
        Mitsunori Komatsu added a comment - I made the following patch. Thanks. $ diff -u JavaConversions.scala.orig JavaConversions.scala — JavaConversions.scala.orig 2012-03-19 01:09:26.000000000 +0900 +++ JavaConversions.scala 2012-06-05 15:11:58.000000000 +0900 @@ -703,7 +703,7 @@ def getKey = k def getValue = v def setValue(v1 : B) = self.put(k, v1) override def hashCode = k.hashCode + v.hashCode + override def hashCode = k.hashCode * 41 + v.hashCode override def equals(other: Any) = other match { case e : ju.Map.Entry [_, _] => k == e.getKey && v == e.getValue case _ => false

          People

          • Assignee:
            Aleksandar Prokopec
            Reporter:
            Mitsunori Komatsu
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development