Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate a good hash code for a list of strings?

Background:

  • I have a short list of strings.
  • The number of strings is not always the same, but are nearly always of the order of a “handful”
  • In our database will store these strings in a 2nd normalised table
  • These strings are never changed once they are written to the database.

We wish to be able to match on these strings quickly in a query without the performance hit of doing lots of joins.

So I am thinking of storing a hash code of all these strings in the main table and including it in our index, so the joins are only processed by the database when the hash code matches.

So how do I get a good hashcode? I could:

  • Xor the hash codes of all the string together
  • Xor with multiply the result after each string (say by 31)
  • Cat all the string together then get the hashcode
  • Some other way

So what do people think?


In the end I just concatenate the strings and compute the hashcode for the concatenation, as it is simple and worked well enough.

(If you care we are using .NET and SqlServer)


Bug!, Bug!

Quoting from Guidelines and rules for GetHashCode by Eric Lippert

The documentation for System.String.GetHashCode notes specifically that two identical strings can have different hash codes in different versions of the CLR, and in fact they do. Don't store string hashes in databases and expect them to be the same forever, because they won't be.

So String.GetHashcode() should not be used for this.

like image 830
Ian Ringrose Avatar asked Apr 28 '10 15:04

Ian Ringrose


People also ask

What makes a good hash code?

hashCode values should be spread as evenly as possible over all ints. hashCode should be relatively quick to compute. hashCode must be deterministic (not random).

How is hash value calculated?

Hashing is simply passing some data through a formula that produces a result, called a hash. That hash is usually a string of characters and the hashes generated by a formula are always the same length, regardless of how much data you feed into it. For example, the MD5 formula always produces 32 character-long hashes.


2 Answers

Standard java practise, is to simply write

final int prime = 31; int result = 1; for( String s : strings ) {     result = result * prime + s.hashCode(); } // result is the hashcode. 
like image 57
Geoff Avatar answered Sep 30 '22 18:09

Geoff


I see no reason not to concatenate the strings and compute the hashcode for the concatenation.

As an analogy, say that I wanted to compute a MD5 checksum for a memory block, I wouldn't split the block up into smaller pieces and compute individual MD5 checksums for them and then combine them with some ad hoc method.

like image 30
Andreas Brinck Avatar answered Sep 30 '22 18:09

Andreas Brinck