Java Hash Code Calculate hashCode(Object value)

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

Description

Returns the hash code of object.

License

Apache License

Parameter

Parameter Description
value the object to get hash code for.

Return

the hash code of value.

Declaration

public static int hashCode(Object value) 

Method Source Code

//package com.java2s;
/*//from w  ww .  ja  v a2 s .co m
 * Copyright 2008-2012 Ruslan Khmelyuk.
 *
 * 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.
 */

public class Main {
    /**
     * Returns the hash code of object. If object is equal {@code null} then 0 is returned.
     *
     * @param value the object to get hash code for.
     * @return the hash code of value.
     */
    public static int hashCode(Object value) {
        if (value != null) {
            return value.hashCode();
        }
        return 0;
    }

    /**
     * Returns the hash code of objects. If any object is equal {@code null} then 0 is returned.
     *
     * @param values the objects to get hash code for.
     * @return the hash code of value.
     *
     * @since 1.0.2
     */
    public static int hashCode(Object... values) {
        if (values == null)
            return 0;

        int result = 0;
        for (Object each : values) {
            result += hashCode(each);
        }
        return result ^ 31;
    }
}

Related

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