Here you can find the source of hashCode(Object obj)
Parameter | Description |
---|---|
obj | the object, may be null |
public static int hashCode(Object obj)
//package com.java2s; /*//from ww w . j av a2 s . com * Copyright 2001-2014 Stephen Colebourne * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Arrays; public class Main { /** * Returns a hash code for an object handling null. * * @param obj the object, may be null * @return the hash code */ public static int hashCode(Object obj) { if (obj == null) { return 0; } if (obj.getClass().isArray()) { if (obj instanceof Object[]) { return Arrays.deepHashCode((Object[]) obj); } else if (obj instanceof int[]) { return Arrays.hashCode((int[]) obj); } else if (obj instanceof long[]) { return Arrays.hashCode((long[]) obj); } else if (obj instanceof byte[]) { return Arrays.hashCode((byte[]) obj); } else if (obj instanceof double[]) { return Arrays.hashCode((double[]) obj); } else if (obj instanceof float[]) { return Arrays.hashCode((float[]) obj); } else if (obj instanceof char[]) { return Arrays.hashCode((char[]) obj); } else if (obj instanceof short[]) { return Arrays.hashCode((short[]) obj); } else if (obj instanceof boolean[]) { return Arrays.hashCode((boolean[]) obj); } } return obj.hashCode(); } /** * Returns a hash code for a {@code boolean}. * * @param value the value to convert to a hash code * @return the hash code */ public static int hashCode(boolean value) { return value ? 1231 : 1237; } /** * Returns a hash code for an {@code int}. * * @param value the value to convert to a hash code * @return the hash code */ public static int hashCode(int value) { return value; } /** * Returns a hash code for a {@code long}. * * @param value the value to convert to a hash code * @return the hash code */ public static int hashCode(long value) { return (int) (value ^ value >>> 32); } /** * Returns a hash code for a {@code float}. * * @param value the value to convert to a hash code * @return the hash code */ public static int hashCode(float value) { return Float.floatToIntBits(value); } /** * Returns a hash code for a {@code double}. * * @param value the value to convert to a hash code * @return the hash code */ public static int hashCode(double value) { return hashCode(Double.doubleToLongBits(value)); } }