Java Object Hash hashCode(Object obj)

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

Description

Returns a hash code for an object handling null.

License

Apache License

Parameter

Parameter Description
obj the object, may be null

Return

the hash code

Declaration

public static int hashCode(Object obj) 

Method Source Code

//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));
    }
}

Related

  1. hashCode(final Object o)
  2. hashCode(final Object... objects)
  3. hashCode(final Object... objects)
  4. hashCode(Object o)
  5. hashCode(Object obj)
  6. hashCode(Object object)
  7. hashCode(Object... args)
  8. hashCode(Object... objects)
  9. makeHashCode(Object... objects)