Java Hash Code Calculate hashCode(Object object)

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

Description

Returns given object's hash code.

License

Apache License

Parameter

Parameter Description
object any object.

Return

hash code of given object.

Declaration

public static int hashCode(Object object) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int INITIAL_HASH = 7;
    private static final int MULTIPLIER = 31;

    /**/*from   w  w w  .  j  av  a  2s. co m*/
     * Returns given object's hash code.
     * 
     * @param object
     *            any object.
     * @return hash code of given object.
     */
    public static int hashCode(Object object) {

        if (object == null) {
            return 0;
        }
        if (object.getClass().isArray()) {
            if ((object instanceof Object[])) {
                return hashCode((Object[]) object);
            }
            if ((object instanceof boolean[])) {
                return hashCode((boolean[]) object);
            }
            if ((object instanceof byte[])) {
                return hashCode((byte[]) object);
            }
            if ((object instanceof char[])) {
                return hashCode((char[]) object);
            }
            if ((object instanceof double[])) {
                return hashCode((double[]) object);
            }
            if ((object instanceof float[])) {
                return hashCode((float[]) object);
            }
            if ((object instanceof int[])) {
                return hashCode((int[]) object);
            }
            if ((object instanceof long[])) {
                return hashCode((long[]) object);
            }
            if ((object instanceof short[])) {
                return hashCode((short[]) object);
            }
        }
        return object.hashCode();
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(Object[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + hashCode(array[i]);
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(boolean[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + hashCode(array[i]);
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(byte[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + array[i];
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(char[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + array[i];
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(double[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + hashCode(array[i]);
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(float[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + hashCode(array[i]);
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(int[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + array[i];
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(long[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + hashCode(array[i]);
        }
        return hash;
    }

    /**
     * Returns given array hash code.
     * 
     * @param array
     *            any object array.
     * @return hash code of given array by elements.
     */
    public static int hashCode(short[] array) {

        if (array == null) {
            return 0;
        }
        int hash = INITIAL_HASH;
        int arraySize = array.length;
        for (int i = 0; i < arraySize; i++) {
            hash = MULTIPLIER * hash + array[i];
        }
        return hash;
    }

    /**
     * Returns given boolean value hash code.
     * 
     * @param bool
     *            any boolean value.
     * @return hash code of given boolean value.
     */
    public static int hashCode(boolean bool) {

        return bool ? 1231 : 1237;
    }

    /**
     * Returns given double value hash code.
     * 
     * @param dbl
     *            any double value.
     * @return hash code of given double value.
     */
    public static int hashCode(double dbl) {

        long bits = Double.doubleToLongBits(dbl);
        return hashCode(bits);
    }

    /**
     * Returns given float value hash code.
     * 
     * @param flt
     *            any float value.
     * @return hash code of given float value.
     */
    public static int hashCode(float flt) {

        return Float.floatToIntBits(flt);
    }

    /**
     * Returns given long value hash code.
     * 
     * @param lng
     *            any long value.
     * @return hash code of given long value.
     */
    public static int hashCode(long lng) {

        return (int) (lng ^ lng >>> 32);
    }
}

Related

  1. hashcode(Object obj)
  2. hashCode(Object obj)
  3. hashCode(Object obj)
  4. hashCode(Object obj)
  5. hashCode(Object obj)
  6. hashCode(Object object)
  7. hashCode(Object object)
  8. hashCode(Object object)
  9. hashCode(Object objects[])