Can anyone think of a way to make a unique hash out of two strings? Something that ensures:
hash(string1,string2) = hash(string2,string1).
I can always store the same reference under two different values in my map, but I thought: There must be a better way...
Another way is to hash both strings and xor the results. Since xor is commutative, the order doesn't matter. If the hashes are equal, don't xor them to avoid collisions with other pairs of identical strings.
Do you want to be fast, or do you want to be good? Any symmetric operation on the individual hash codes will produce what you want; +
, *
, and ^
are all decent choices; ^
produces 0 if the two are the same, so you generally need an if
to catch that; +
is more likely to generate collisions than *
but both are not so great given that the intrinsic hashCode
method on String
is pretty lousy:
scala> "BB".hashCode == "Aa".hashCode // Seriously?!
res40: Boolean = true
If you want your strings to not collide so much, use scala.util.MurmurHash.stringHash
on the strings (2.9; scala.util.hashing.MurmurHash.stringHash
in 2.10), and then one of the above methods.
Well you could try "ordering" both strings before hashing them, so that any pair of strings will always be processed in the same order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With