Java Hash Calculate hash(int aSeed, Object[] aArray)

Here you can find the source of hash(int aSeed, Object[] aArray)

Description

Arrays of Objects.

License

Creative Commons License

Declaration

public static int hash(int aSeed, Object[] aArray) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    private static final int fODD_PRIME_NUMBER = 37;

    /** booleans.  */
    public static int hash(int aSeed, boolean aBoolean) {
        return firstTerm(aSeed) + (aBoolean ? 1 : 0);
    }//from  www . j a  v  a2 s.c o m

    /** chars.  */
    public static int hash(int aSeed, char aChar) {
        return firstTerm(aSeed) + (int) aChar;
    }

    /**
    * ints.
    *
    * Note that byte and short are handled by this method, through
    * implicit conversion.
    */
    public static int hash(int aSeed, int aInt) {
        return firstTerm(aSeed) + aInt;
    }

    /** longs.  */
    public static int hash(int aSeed, long aLong) {
        return firstTerm(aSeed) + (int) (aLong ^ (aLong >>> 32));
    }

    /** floats.  */
    public static int hash(int aSeed, float aFloat) {
        return hash(aSeed, Float.floatToIntBits(aFloat));
    }

    /** doubles.  */
    public static int hash(int aSeed, double aDouble) {
        return hash(aSeed, Double.doubleToLongBits(aDouble));
    }

    /** Possibly-null Object fields.  */
    public static int hash(int aSeed, Object aObject) {
        return hash(aSeed, (aObject == null ? 0 : aObject.hashCode()));
    }

    /** Arrays of Objects.  */
    public static int hash(int aSeed, Object[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of booleans.  */
    public static int hash(int aSeed, boolean[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of chars.  */
    public static int hash(int aSeed, char[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of bytes.  */
    public static int hash(int aSeed, byte[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of shorts.  */
    public static int hash(int aSeed, short[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of integers. */
    public static int hash(int aSeed, int[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of longs  */
    public static int hash(int aSeed, long[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of floats.  */
    public static int hash(int aSeed, float[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    /** Arrays of doubles.  */
    public static int hash(int aSeed, double[] aArray) {
        int result = aSeed;
        for (int idx = 0; idx < aArray.length; ++idx) {
            result = hash(result, aArray[idx]);
        }
        return result;
    }

    private static int firstTerm(int aSeed) {
        return fODD_PRIME_NUMBER * aSeed;
    }
}

Related

  1. hash(final Object key, final Object value)
  2. hash(final Object object)
  3. hash(final Object[] objects)
  4. hash(finalObject object)
  5. hash(int aSeed, boolean aBoolean)
  6. hash(int base, boolean field)
  7. hash(int h)
  8. hash(int h)
  9. hash(int h, boolean v)