Class HashCodeUtil


  • public final class HashCodeUtil
    extends Object
    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 int SEED
      An initial value for a hashCode, to which is added contributions from fields.
    • Constructor Summary

      Constructors 
      Constructor Description
      HashCodeUtil()  
    • Field Detail

      • 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 collisions of hashCode values.
        See Also:
        Constant Field Values
    • Constructor Detail

      • HashCodeUtil

        public HashCodeUtil()
    • Method Detail

      • hash

        public static int hash​(int aSeed,
                               boolean aBoolean)
        Parameters:
        aSeed - initial value of the hashCode
        aBoolean - value to contribute to the hashCode
        Returns:
        hash code for a boolean.
      • hash

        public static int hash​(int aSeed,
                               char aChar)
        Parameters:
        aSeed - initial value of the hashCode
        aChar - value to contribute to the hashCode
        Returns:
        has code for a char value.
      • hash

        public static int hash​(int aSeed,
                               int aInt)
        Parameters:
        aSeed - initial value of the hashCode
        aInt - value to contribute to the hashCode
        Returns:
        hash code for int.
      • hash

        public static int hash​(int aSeed,
                               long aLong)
        Parameters:
        aSeed - initial value of the hashCode
        aLong - value to contribute to the hashCode
        Returns:
        hash code for a long value.
      • hash

        public static int hash​(int aSeed,
                               float aFloat)
        Parameters:
        aSeed - initial value of the hashCode
        aFloat - value to contribute to the hashCode
        Returns:
        a hash code for a float value.
      • hash

        public static int hash​(int aSeed,
                               double aDouble)
        Parameters:
        aSeed - initial value of the hashCode
        aDouble - value to contribute to the hashCode
        Returns:
        a hash code for a double value.
      • hash

        public static int hash​(int aSeed,
                               Object aObject)
        aObject is a possibly-null object field, and possibly an array. If aObject is an array, then each element may be a primitive or a possibly-null object.
        Parameters:
        aSeed - initial value of the hashCode
        aObject - value to contribute to the hashCode
        Returns:
        a hash code for an Object (Array-aware).