Java Hash Calculate hash(Object[] array)

Here you can find the source of hash(Object[] array)

Description

calculate the array hash (only the first level)

License

LGPL

Declaration

public static int hash(Object[] array) 

Method Source Code

//package com.java2s;
/*/*from  w ww . ja va  2s. c  o m*/
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

public class Main {
    private static int SEED = 23;
    private static int PRIME_NUMER = 37;

    /**
     * calculate the array hash (only the first level)
     */
    public static int hash(Object[] array) {
        int length = array.length;
        int seed = SEED;
        for (Object anArray : array) {
            seed = hash(seed, anArray == null ? 0 : anArray.hashCode());
        }
        return seed;
    }

    /**
     * calculate the array hash (only the first level)
     */
    public static int hash(char[] array) {
        int length = array.length;
        int seed = SEED;
        for (char anArray : array) {
            seed = hash(seed, anArray);
        }
        return seed;
    }

    /**
     * calculate the array hash (only the first level)
     */
    public static int hash(byte[] bytes) {
        int length = bytes.length;
        int seed = SEED;
        for (byte aByte : bytes) {
            seed = hash(seed, aByte);
        }
        return seed;
    }

    private static int hash(int seed, int i) {
        return PRIME_NUMER * seed + i;
    }
}

Related

  1. hash(Object value)
  2. hash(Object value, int seed)
  3. hash(Object... as)
  4. hash(Object... value)
  5. hash(Object... values)
  6. hash(Object[] array)
  7. hash(Object[] state)
  8. hash1(int val)
  9. hash1(Object a)