Class HashCodeUtils

java.lang.Object
ucar.unidata.util.HashCodeUtils

public class HashCodeUtils extends Object
Taken from http://www.javapractices.com/Topic28.cjp.

Collected methods which allow easy implementation of hashCode.

Example use case.

  public int hashCode(){
    int result = HashCodeUtil.SEED;
    //collect the contributions of various fields
    result = HashCodeUtil.hash(result, fPrimitive);
    result = HashCodeUtil.hash(result, fObject);
    result = HashCodeUtil.hash(result, fArray);
    return result;
  }
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    An initial value for a hashCode, to which is added contributions from fields.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hash(int aSeed, boolean aBoolean)
    booleans.
    static int
    hash(int aSeed, char aChar)
    chars.
    static int
    hash(int aSeed, double aDouble)
    doubles.
    static int
    hash(int aSeed, float aFloat)
    floats.
    static int
    hash(int aSeed, int aInt)
    ints.
    static int
    hash(int aSeed, long aLong)
    longs.

    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 is added contributions from fields. Using a non-zero value decreases collisons of hashCode values.
      See Also:
  • Constructor Details

    • HashCodeUtils

      public HashCodeUtils()
  • Method Details

    • hash

      public static int hash(int aSeed, boolean aBoolean)
      booleans.
      Parameters:
      aSeed - _more_
      aBoolean - _more_
      Returns:
      _more_
    • hash

      public static int hash(int aSeed, char aChar)
      chars.
      Parameters:
      aSeed - _more_
      aChar - _more_
      Returns:
      _more_
    • hash

      public static int hash(int aSeed, int aInt)
      ints.
      Parameters:
      aSeed - _more_
      aInt - _more_
      Returns:
      _more_
    • hash

      public static int hash(int aSeed, long aLong)
      longs.
      Parameters:
      aSeed - _more_
      aLong - _more_
      Returns:
      _more_
    • hash

      public static int hash(int aSeed, float aFloat)
      floats.
      Parameters:
      aSeed - _more_
      aFloat - _more_
      Returns:
      _more_
    • hash

      public static int hash(int aSeed, double aDouble)
      doubles.
      Parameters:
      aSeed - _more_
      aDouble - _more_
      Returns:
      _more_