Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make a unique hash out of two strings

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...

like image 265
Peter Avatar asked Oct 22 '12 20:10

Peter


3 Answers

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.

like image 117
Kim Stebel Avatar answered Nov 15 '22 20:11

Kim Stebel


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.

like image 33
Rex Kerr Avatar answered Nov 15 '22 20:11

Rex Kerr


Well you could try "ordering" both strings before hashing them, so that any pair of strings will always be processed in the same order.

like image 40
Juan Campa Avatar answered Nov 15 '22 19:11

Juan Campa