Java Object Hash hashCode(final Object... objects)

Here you can find the source of hashCode(final Object... objects)

Description

Compute a combined hash code from the supplied objects.

License

Open Source License

Parameter

Parameter Description
objects the objects used to construct the hashcode (can contain <code>null</code> objects)

Return

the hashcode

Declaration

public static final int hashCode(final Object... objects) 

Method Source Code

//package com.java2s;
/*/*w ww  .  j ava  2 s  .  c om*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import java.util.Arrays;

public class Main {
    /**
     * Used when calculating hashcodes.
     *
     * @see #hashCode(Object...)
     */
    private static final int PRIME = 103;

    /**
     * Compute a combined hash code from the supplied objects. This method always returns 0 if no objects are supplied.
     *
     * @param objects the objects used to construct the hashcode (can contain <code>null</code> objects)
     * @return the hashcode
     */
    public static final int hashCode(final Object... objects) {
        if ((objects == null) || (objects.length == 0)) {
            return 0;
        }

        // Compute the hash code for all of the objects ...
        int hc = 0;

        for (Object object : objects) {
            hc = PRIME * hc;

            if (object instanceof byte[]) {
                hc += Arrays.hashCode((byte[]) object);
            } else if (object instanceof boolean[]) {
                hc += Arrays.hashCode((boolean[]) object);
            } else if (object instanceof short[]) {
                hc += Arrays.hashCode((short[]) object);
            } else if (object instanceof int[]) {
                hc += Arrays.hashCode((int[]) object);
            } else if (object instanceof long[]) {
                hc += Arrays.hashCode((long[]) object);
            } else if (object instanceof float[]) {
                hc += Arrays.hashCode((float[]) object);
            } else if (object instanceof double[]) {
                hc += Arrays.hashCode((double[]) object);
            } else if (object instanceof char[]) {
                hc += Arrays.hashCode((char[]) object);
            } else if (object instanceof Object[]) {
                hc += Arrays.hashCode((Object[]) object);
            } else if (object != null) {
                hc += object.hashCode();
            }
        }

        return hc;
    }
}

Related

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