Class JenkinsHash

java.lang.Object
org.apache.activemq.util.JenkinsHash

public class JenkinsHash extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
    int
    hash(byte[] bytes)
    Calculate a hash using all bytes from the input argument, and a seed of -1.
    int
    hash(byte[] bytes, int initVal)
    Calculate a hash using all bytes from the input argument, and a seed of -1.
    int
    hash(byte[] key, int nbytes, int initval)
    taken from hashlittle() -- hash a variable-length key into a 32-bit value

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JenkinsHash

      public JenkinsHash()
  • Method Details

    • getInstance

      public static JenkinsHash getInstance()
    • hash

      public int hash(byte[] bytes)
      Calculate a hash using all bytes from the input argument, and a seed of -1.
      Parameters:
      bytes - input bytes
      Returns:
      hash value
    • hash

      public int hash(byte[] bytes, int initVal)
      Calculate a hash using all bytes from the input argument, and a seed of -1.
      Parameters:
      bytes - input bytes
      Returns:
      hash value
    • hash

      public int hash(byte[] key, int nbytes, int initval)
      taken from hashlittle() -- hash a variable-length key into a 32-bit value
      Parameters:
      key - the key (the unaligned variable-length array of bytes)
      nbytes - number of bytes to include in hash
      initval - can be any integer value
      Returns:
      a 32-bit value. Every bit of the key affects every bit of the return value. Two keys differing by one or two bits will have totally different hash values.

      The best hash table sizes are powers of 2. There is no need to do mod a prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. For example, if you need only 10 bits, do h = (h & hashmask(10)); In which case, the hash table should have hashsize(10) elements.

      If you are hashing n strings byte[][] k, do it like this: for (int i = 0, h = 0; i < n; ++i) h = hash( k[i], h);

      By Bob Jenkins, 2006. [email protected]. You may use this code any way you wish, private, educational, or commercial. It's free.

      Use for hash table lookup, or anything where one collision in 2^^32 is acceptable. Do NOT use for cryptographic purposes.