Here you can find the source of hashCode(Object object)
Parameter | Description |
---|---|
object | any object. |
public static int hashCode(Object object)
//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); } }