Java Hash Calculate hash(final Object[] objects)

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

Description

Compute a hash code for a collection of objects from their individual hash codes.

License

Open Source License

Parameter

Parameter Description
objects from which hash code to be computed.

Return

hash code.

Declaration

public static int hash(final Object[] objects) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  ww .ja va  2  s  . co  m
     * Compute a hash code for a collection of objects from their individual hash codes.
     * @param objects from which hash code to be computed.
     * @return hash code.
     */
    public static int hash(final Object[] objects) {
        if (objects.length == 0) {
            return 0;
        }
        final Object obj = objects[0];
        int hash = (obj == null ? 0 : objects[0].hashCode()) + 43;
        for (int i = 1; i < objects.length; i++) {
            final Object ob = objects[i];
            final int h = ob == null ? 0 : ob.hashCode();
            hash = pairHash(hash, h);
        }
        return hash;
    }

    /**
     * Produce a one-one onto mapping from a pair of integers to an integer.
     * Only fails because of finite precision of integers. Taken to be a good way of
     * combining hashes.
     * @param i first integer.
     * @param j second integer.
     * @return combined integer.
     */
    public static int pairHash(final int i, final int j) {
        if (i == 0 && j == 0) {
            return 1;
        }
        final long li = (long) i;
        final long lj = (long) j;
        final long k = (li < 0 ? -li : li) + (lj < 0 ? -lj : lj);
        assert k >= 0 : k;
        final long l = i >= 0 ? k + j : 3 * k - j;
        final long x = ((k * (k - 1L)) << 1) + l + 2L;
        return (int) x;
    }

    /**
     * Produce a one-one onto mapping from three integers to an integer.
     * Only fails because of finite precision of integers. Taken to be a good way of
     * combining hashes.
     * @param i first integer.
     * @param j second integer.
     * @param k third integer.
     * @return combined integer.
     */
    public static int pairHash(int i, int j, int k) {
        final int t = pairHash(i, j);
        return pairHash(t, k);
    }

    /**
     * Produce a one-one onto mapping from four integers to an integer.
     * Only fails because of finite precision of integers. Taken to be a good way of
     * combining hashes.
     * @param i first integer.
     * @param j second integer.
     * @param k third integer.
     * @param l fourth integer.
     * @return combined integer.
     */
    public static int pairHash(int i, int j, int k, int l) {
        int t = pairHash(i, j);
        t = pairHash(t, k);
        return pairHash(t, l);
    }

    /**
     * Produce a one-one onto mapping from five integers to an integer.
     * Only fails because of finite precision of integers. Taken to be a good way of
     * combining hashes.
     * @param i first integer.
     * @param j second integer.
     * @param k third integer.
     * @param l fourth integer.
     * @param m fifth integer.
     * @return combined integer.
     */
    public static int pairHash(int i, int j, int k, int l, int m) {
        int t = pairHash(i, j);
        t = pairHash(t, k);
        t = pairHash(t, l);
        return pairHash(t, m);
    }
}

Related

  1. hash(double value)
  2. hash(final boolean value)
  3. hash(final int value)
  4. hash(final Object key, final Object value)
  5. hash(final Object object)
  6. hash(finalObject object)
  7. hash(int aSeed, boolean aBoolean)
  8. hash(int aSeed, Object[] aArray)
  9. hash(int base, boolean field)