Scala Set Hashcode
Assume we have three sets of strings in Scala. One has elements A,B,C. Two has elements B,C,D. And Three has elements J,K,I.
My first开发者_开发百科 question is, is there any way that the hashcodes for any two of these sets could be the same? My second question is, if I add D to One and A to Two to get new Sets One.n and Two.n, are the hashcodes for One.n and Two.n the same?
Question 1) In general yes, entirely possible. A hashcode is a limited number of bytes long. A Set can be any size. So hashcodes cannot be unique (although usually they are).
Question 2) Why not try it?
scala> val One = collection.mutable.Set[String]("A", "B", "C")
One: scala.collection.mutable.Set[String] = Set(A, B, C)
scala> One.hashCode
res3: Int = 1491157345
scala> val Two = collection.mutable.Set[String]("B", "C", "D")
Two: scala.collection.mutable.Set[String] = Set(B, D, C)
scala> Two.hashCode
res4: Int = -967442916
scala> One += "D"
res5: One.type = Set(A, B, D, C)
scala> Two += "A"
res6: Two.type = Set(B, D, A, C)
scala> One.hashCode
res7: Int = -232075924
scala> Two.hashCode
res8: Int = -232075924
So, yes they are, as you might expect, since you would expect the ==
method to be true for these two instances.
Sets which are equal and don't have anything strange inside them (i.e. anything with an unstable hash code, or where the hash code is inconsistent with equals) should have equal hash codes. If this is not true, and the sets are the same type of set, it is a bug and should be reported. If the sets are different types of sets, it may or may not be a bug to have different hash codes (but in any case it should agree with equals). I am not aware of any cases where different set implementations are not equal (e.g. even mutable BitSet agrees with immutable Set), however.
So:
- hashCode is never guaranteed to be unique, but it should be well-distributed in that the probability of collisions should be low
- hashCode of sets should always be consistent with equals (as long as everything you put in the set has hashCode consistent with equals) in that equal sets have equal hash codes. (The converse is not true because of point (1).)
- Sets care only about the identity of the contents, not the order of addition to the set (that's the point of having a set instead of, say, a List)
精彩评论