Java Hash Code Calculate hashCode(Object array)

Here you can find the source of hashCode(Object array)

Description

hash Code

License

Open Source License

Declaration

public static int hashCode(Object array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int hashCode(Object array) {
        if (array == null) {
            return 0;
        } else if (!array.getClass().isArray()) {
            return array.hashCode();
        } else {//  w  w  w.j a  v a  2 s  .  c om
            int hashCode = 17;
            int i;
            if (array instanceof long[]) {
                long[] longArray = (long[]) ((long[]) array);

                for (i = 0; i < longArray.length; ++i) {
                    hashCode = hashCode * 37 + (int) (longArray[i] ^ longArray[i] >> 32);
                }
            } else if (array instanceof int[]) {
                int[] intArray = (int[]) ((int[]) array);

                for (i = 0; i < intArray.length; ++i) {
                    hashCode = hashCode * 37 + intArray[i];
                }
            } else if (array instanceof short[]) {
                short[] shortArray = (short[]) ((short[]) array);

                for (i = 0; i < shortArray.length; ++i) {
                    hashCode = hashCode * 37 + shortArray[i];
                }
            } else if (array instanceof byte[]) {
                byte[] byteArray = (byte[]) ((byte[]) array);

                for (i = 0; i < byteArray.length; ++i) {
                    hashCode = hashCode * 37 + byteArray[i];
                }
            } else if (array instanceof double[]) {
                double[] doubleArray = (double[]) ((double[]) array);

                for (i = 0; i < doubleArray.length; ++i) {
                    long longBits = Double.doubleToLongBits(doubleArray[i]);
                    hashCode = hashCode * 37 + (int) (longBits ^ longBits >> 32);
                }
            } else if (array instanceof float[]) {
                float[] floatArray = (float[]) ((float[]) array);

                for (i = 0; i < floatArray.length; ++i) {
                    hashCode = hashCode * 37 + Float.floatToIntBits(floatArray[i]);
                }
            } else if (array instanceof boolean[]) {
                boolean[] booleanArray = (boolean[]) ((boolean[]) array);

                for (i = 0; i < booleanArray.length; ++i) {
                    hashCode = hashCode * 37 + (booleanArray[i] ? 1 : 0);
                }
            } else if (array instanceof char[]) {
                char[] charArray = (char[]) ((char[]) array);

                for (i = 0; i < charArray.length; ++i) {
                    hashCode = hashCode * 37 + charArray[i];
                }
            } else {
                Object[] objectArray = (Object[]) ((Object[]) array);

                for (i = 0; i < objectArray.length; ++i) {
                    hashCode = hashCode * 37 + hashCode(objectArray[i]);
                }
            }

            return hashCode;
        }
    }
}

Related

  1. hashCode(long longVal)
  2. hashCode(long value)
  3. hashCode(long x)
  4. hashCode(long[] a, int fromIndex, int toIndex)
  5. hashCode(Object a)
  6. hashCode(Object o)
  7. hashCode(Object o)
  8. hashCode(Object o)
  9. hashCode(Object o1, Object o2)