Java Hash Code Calculate hashCode(final int i)

Here you can find the source of hashCode(final int i)

Description

Provides a hash code based on the given integer value.

License

Open Source License

Parameter

Parameter Description
i The integer value

Return

i

Declaration

public static final int hashCode(final int i) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  www .j  a v a2s  .  c  o  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Provides a hash code based on the given integer value.
     *
     * @param i
     *            The integer value
     * @return <code>i</code>
     */
    public static final int hashCode(final int i) {
        return i;
    }

    /**
     * Provides a hash code for the object -- defending against <code>null</code>.
     *
     * @param object
     *            The object for which a hash code is required.
     * @return <code>object.hashCode</code> or <code>0</code> if <code>object</code> if
     *         <code>null</code>.
     */
    public static final int hashCode(final Object object) {
        return object != null ? object.hashCode() : 0;
    }

    /**
     * Computes the hash code for an array of objects, but with defense against <code>null</code>.
     *
     * @param objects
     *            The array of objects for which a hash code is needed; may be <code>null</code>.
     * @return The hash code for <code>objects</code>; or <code>0</code> if <code>objects</code> is
     *         <code>null</code>.
     */
    public static final int hashCode(final Object[] objects) {
        if (objects == null) {
            return 0;
        }

        int hashCode = 89;
        for (int i = 0; i < objects.length; i++) {
            final Object object = objects[i];
            if (object != null) {
                hashCode = hashCode * 31 + object.hashCode();
            }
        }

        return hashCode;
    }
}

Related

  1. hashCode(double dbl)
  2. hashCode(double v)
  3. hashCode(double val)
  4. hashCode(final byte[] data)
  5. hashCode(final char[] text, final int textOffset, final int textLen)
  6. hashCode(final int i)
  7. hashCode(final int seed, final int hashcode)
  8. hashCode(final int x, final int y)
  9. hashcode(final int[] array)