Java Object Hash safeHashCode(Object o)

Here you can find the source of safeHashCode(Object o)

Description

Returns the hash-code of the specified object.

License

MIT License

Parameter

Parameter Description
o the object.

Return

the object's hash-code, or 1 if the object is null.

Declaration

public static int safeHashCode(Object o) 

Method Source Code

//package com.java2s;
/*/*from  w w  w  .  ja v a 2  s.  co  m*/
 * Copyright (c) 2013-2017 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */

import java.util.Arrays;

public class Main {
    /**
     * Returns the hash-code of the specified object. If the specified object
     * is null, then this method returns 1, rather then throwing NullPointerException.
     *
     * @param o the object.
     * @return the object's hash-code, or 1 if the object is null.
     */
    public static int safeHashCode(Object o) {
        int hash = 1;

        if (o != null) {
            if (o.getClass().isArray())
                return Arrays.deepHashCode((Object[]) o);
            else
                hash = o.hashCode();
        }

        return hash;
    }
}

Related

  1. hashCode(Object obj)
  2. hashCode(Object object)
  3. hashCode(Object... args)
  4. hashCode(Object... objects)
  5. makeHashCode(Object... objects)
  6. safeHashCode(Object... objects)