Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaConversions.MapWrapper generates wrong hashCode eventually #5880

Closed
scabug opened this issue Jun 5, 2012 · 3 comments
Closed

JavaConversions.MapWrapper generates wrong hashCode eventually #5880

scabug opened this issue Jun 5, 2012 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jun 5, 2012

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
@scabug
Copy link
Author

scabug commented Jun 5, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5880?orig=1
Reporter: Mitsunori Komatsu (komamitsu)
Affected Versions: 2.9.2
Attachments:

  • App.scala (created on Jun 5, 2012 6:04:12 AM UTC, 571 bytes)

@scabug
Copy link
Author

scabug commented Jun 5, 2012

Mitsunori Komatsu (komamitsu) said:
I think this issue is not "Blocker". But I couldn't change the priority...

@scabug
Copy link
Author

scabug commented Jun 5, 2012

Mitsunori Komatsu (komamitsu) said:
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
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants