Computing hash codes : Hash Code « Development Class « Java

Computing hash codes

 * JBoss DNA (
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * See the AUTHORS.txt file in the distribution for a full listing of 
 * individual contributors. 
 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
 * is licensed to you under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * JBoss DNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site:

import java.util.Arrays;

 * Utilities for easily computing hash codes. The algorithm should generally produce good distributions for use in hash-based
 * containers or collections, but as expected does always result in repeatable hash codes given the inputs.
 * @author Randall Hauch
public class HashCode {

    // Prime number used in improving distribution: 1,000,003
    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 that should be used to compute the hash code
     * @return the hash code
    public static int compute( Object... objects ) {
        return compute(0, objects);

     * Compute a combined hash code from the supplied objects using the supplied seed.
     * @param seed a value upon which the hash code will be based; may be 0
     * @param objects the objects that should be used to compute the hash code
     * @return the hash code
    protected static int compute( int seed, Object... objects ) {
        if (objects == null || objects.length == 0) {
            return seed * HashCode.PRIME;
        // Compute the hash code for all of the objects ...
        int hc = seed;
        for (Object object : objects) {
            hc = HashCode.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 examples in the same category

1.A hash-code generator and a collection of static hash-code generation methods.
2.MD5 hash generator
3.Hash 32 String
4.Hash 64 String
5.MD5 hashing: Encodes a string
6.MD5 String
7.Hash Code BuilderHash Code Builder
8.HashCode generationHashCode generation
9.Get hash code for primitive data types
10.Return as hash code for the given object
11.Null Safe Hash Code
12.A very efficient java hash algorithm, based on the BuzHash algoritm
13.Easy implementation of hashCode
14.An implementation of the HMACT64 keyed hashing algorithm
15.Gets the hash code of an object returning zero when the object is null
16.Unify Hash
17.Secure Hash
18.FNV Hash
19.Jenkins Hash
20.Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution.
21.Concurrent Cuckoo hashing using lock striping.
22.encode Hex
23.Fowler/Noll/Vo hash algorhythm
24.Produces 32-bit hash for hash table lookup. (Jenkins Hash Function)
25.Key Value Hash
26.Paul Hsieh's Hash Function.
27.An extension of WeakReference that implements a sane equals and hashcode method.
28.Dual Key Hash Map
29.A hash map with int key and int values.
30.null Safe Equals and Hash
31.Generates a hash code for a given source code.
32.AtkinsonHash utility class implements the hash algorithm used by HyperCard's ask password command.
33.Hash Code AssistHash Code Assist
34.This is a very fast, non-cryptographic hash suitable for general hash-based lookup.
35.An advanced hash table supporting configurable garbage collection semantics of keys and values
36.Hash string