Computing hash codes : Hash Code « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Hash CodeScreenshots 
Computing hash codes
      
/*
 * JBoss DNA (http://www.jboss.org/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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * 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: http://www.fsf.org.
 */

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 computeObject... 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 computeint 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.