W3docs

Good Hash Function for Strings

A good hash function for strings should have the following properties:

A good hash function for strings should have the following properties:

  1. Uniformity: The function should distribute the strings uniformly across the hash table.
  2. Efficiency: The function should be efficient to compute.
  3. Avalanche effect: The function should produce significantly different hash values for small changes in the input string to ensure uniform distribution.
  4. Minimizes collisions: The function should minimize collisions, though they are inevitable and must be handled by resolution strategies like chaining or open addressing.

One widely used hash function for strings is the DJB2 hash function. It has good performance and a low collision rate. Note that DJB2 is designed for general-purpose hashing and is not suitable for cryptographic security. While Java's built-in String.hashCode() is typically preferred for standard applications, DJB2 remains a popular custom alternative. Here is the Java implementation of the DJB2 hash function:


public static int djb2(String str) {
    int hash = 5381;
    for (int i = 0; i < str.length(); i++) {
        hash = ((hash << 5) + hash) + str.charAt(i);
    }
    return hash;
}

You can use the DJB2 hash function as follows:


int hash = djb2("hello");
System.out.println(hash); // prints "99162322"

I hope this helps. Let me know if you have any questions.