Class Hashcoder

java.lang.Object
org.biojava.nbio.core.util.Hashcoder

public class Hashcoder extends Object
Contains helper methods for generating a HashCode without having to resort to the commons lang hashcode builders. Example where the property name is a String and the property age is an int
 public int hashCode() {
   int result = Hashcoder.SEED;
   result = Hashcoder.hash(result, this.getName());
   result = Hashcoder.hash(result, this.getAge());
   return result;
 }
 
Author:
ayates
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The prime number used to multiply any calculated hashcode seed by i.e.
    static final int
    An initial value for a hashCode, to which we add contributions from fields.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hash(int seed, boolean b)
     
    static int
    hash(int seed, char c)
     
    static int
    hash(int seed, double d)
    double support which is done using the same techinque as float hashing except we convert to a long not to an int.
    static int
    hash(int seed, float f)
    float support done via Float.floatToIntBits(float) which allows the encoding of a float as an int.
    static int
    hash(int seed, int i)
    Used for ints, bytes and shorts
    static int
    hash(int seed, long l)
    long support done by shifting by 32 (using unsigned shift)
    static int
    hash(int seed, Object o)
    o is a possibly-null object field, and possibly an array.

    Methods inherited from class java.lang.Object

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

    • SEED

      public static final int SEED
      An initial value for a hashCode, to which we add contributions from fields. Using a non-zero value decreases collisions of hashCode values.
      See Also:
    • PRIME

      public static final int PRIME
      The prime number used to multiply any calculated hashcode seed by i.e. result = PRIME*result + c Where result is the result of the previous calculation (at first this will be seed) and c is the calculated int to add to result
      See Also:
  • Constructor Details

    • Hashcoder

      public Hashcoder()
  • Method Details

    • hash

      public static int hash(int seed, boolean b)
    • hash

      public static int hash(int seed, char c)
    • hash

      public static int hash(int seed, int i)
      Used for ints, bytes and shorts
    • hash

      public static int hash(int seed, long l)
      long support done by shifting by 32 (using unsigned shift)
    • hash

      public static int hash(int seed, float f)
      float support done via Float.floatToIntBits(float) which allows the encoding of a float as an int. We can then hash as normal.
    • hash

      public static int hash(int seed, double d)
      double support which is done using the same techinque as float hashing except we convert to a long not to an int.
    • hash

      public static int hash(int seed, Object o)
      o is a possibly-null object field, and possibly an array. If o is an array, then each element may be a primitive or a possibly-null object.