Easy implementation of hashCode : 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 
Easy implementation of hashCode
     
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


/**
 * Collected methods which allow easy implementation of <code>hashCode()</code>.
 * Based on items #7 and #8 from "Effective Java" book.
 * <p>
 * Usage scenario:<br>
 * <pre>
 * int result = HashCodeUtil.SEED;
 * result = HashCodeUtil.hash(result, fIsDecrepit);
 * ...
 * return result;
 * </pre>
 */
public class HashCode {

  /**
   * An initial hash code value to which is added contributions from fields.
   * Using a non-zero value decreases collisions of hash code values.
   */
  public static final int SEED = 173;

  public static final int PRIME = 37;

  // ---------------------------------------------------------------- boolean

  /**
   * Calculates hash code for booleans.
   */
  public static int hash(int seed, boolean aBoolean) {
    return (PRIME * seed(aBoolean ? 1231 1237);
  }

  /**
   * Calculates hash code for boolean array.
   */
  public static int hash(int seed, boolean[] booleanArray) {
    if (booleanArray == null) {
      return 0;
    }
    for (boolean aBoolean : booleanArray) {
      seed = hash(seed, aBoolean);
    }
    return seed;
  }

  /**
   * Calculates hash code for boolean array.
   */
  public static int hashBooleanArray(int seed, boolean... booleanArray) {
    return hash(seed, booleanArray);
  }

  // ---------------------------------------------------------------- char

  /**
   * Calculates hash code for chars.
   */
  public static int hash(int seed, char aChar) {
    return (PRIME * seed(intaChar;
  }

  /**
   * Calculates hash code for char array.
   */
  public static int hash(int seed, char[] charArray) {
    if (charArray == null) {
      return 0;
    }
    for (char aChar : charArray) {
      seed = hash(seed, aChar);
    }
    return seed;
  }

  /**
   * Calculates hash code for char array.
   */
  public static int hashCharArray(int seed, char... charArray) {
    return hash(seed, charArray);
  }

  // ---------------------------------------------------------------- ints

  /**
   * Calculates hash code for ints.
   */
  public static int hash(int seed, int anInt) {
    return (PRIME * seed+ anInt;
  }

  /**
   * Calculates hash code for int array.
   */
  public static int hash(int seed, int[] intArray) {
    if (intArray == null) {
      return 0;
    }
    for (int anInt : intArray) {
      seed = hash(seed, anInt);
    }
    return seed;
  }

  /**
   * Calculates hash code for int array.
   */
  public static int hashIntArray(int seed, int... intArray) {
      return hash(seed, intArray);
  }


  /**
   * Calculates hash code for short array.
   */
  public static int hash(int seed, short[] shortArray) {
    if (shortArray == null) {
      return 0;
    }
    for (short aShort : shortArray) {
      seed = hash(seed, aShort);
    }
    return seed;
  }

  /**
   * Calculates hash code for short array.
   */
  public static int hashShortArray(int seed, short... shortArray) {
    return hash(seed, shortArray);
  }

  /**
   * Calculates hash code for byte array.
   */
  public static int hash(int seed, byte[] byteArray) {
    if (byteArray == null) {
      return 0;
    }
    for (byte aByte : byteArray) {
      seed = hash(seed, aByte);
    }
    return seed;
  }

  /**
   * Calculates hash code for byte array.
   */
  public static int hashByteArray(int seed, byte... byteArray) {
    return hash(seed, byteArray);
  }


  // ---------------------------------------------------------------- long

  /**
   * Calculates hash code for longs.
   */
  public static int hash(int seed, long aLong) {
    return (PRIME * seed(int) (aLong ^ (aLong >>> 32));
  }

  /**
   * Calculates hash code for long array.
   */
  public static int hash(int seed, long[] longArray) {
    if (longArray == null) {
      return 0;
    }
    for (long aLong : longArray) {
      seed = hash(seed, aLong);
    }
    return seed;
  }

  /**
   * Calculates hash code for long array.
   */
  public static int hashLongArray(int seed, long... longArray) {
    return hash(seed, longArray);
  }

  // ---------------------------------------------------------------- float

  /**
   * Calculates hash code for floats.
   */
  public static int hash(int seed, float aFloat) {
    return hash(seed, Float.floatToIntBits(aFloat));
  }

  /**
   * Calculates hash code for float array.
   */
  public static int hash(int seed, float[] floatArray) {
    if (floatArray == null) {
      return 0;
    }
    for (float aFloat : floatArray) {
      seed = hash(seed, aFloat);
    }
    return seed;
  }


  /**
   * Calculates hash code for float array.
   */
  public static int hashFloatArray(int seed, float... floatArray) {
    return hash(seed, floatArray);
  }

  // ---------------------------------------------------------------- double

  /**
   * Calculates hash code for doubles.
   */
  public static int hash(int seed, double aDouble) {
    return hash(seed, Double.doubleToLongBits(aDouble));
  }

  /**
   * Calculates hash code for double array.
   */
  public static int hash(int seed, double[] doubleArray) {
    if (doubleArray == null) {
      return 0;
    }
    for (double aDouble : doubleArray) {
      seed = hash(seed, aDouble);
    }
    return seed;
  }

  /**
   * Calculates hash code for double array.
   */
  public static int hashDoubleArray(int seed, double... doubleArray) {
      return hash(seed, doubleArray);
  }

  // ---------------------------------------------------------------- object

  /**
   * Calculates hash code for Objects. Object is a possibly-null object field, and possibly an array.
   * <p>
   * If <code>aObject</code> is an array, then each element may be a primitive
   * or a possibly-null object.
   */
  public static int hash(int seed, Object aObject) {
    int result = seed;
    if (aObject == null) {
      result = hash(result, 0);
    else if (aObject.getClass().isArray() == false) {
      result = hash(result, aObject.hashCode());
    else {
      Object[] objects = (Object[]) aObject;
      int length = objects.length;
      for (int idx = 0; idx < length; ++idx) {
        result = hash(result, objects[idx]);
      }
    }
    return result;
  }

}

   
    
    
    
    
  
Related examples in the same category
1. Computing hash codes
2. A hash-code generator and a collection of static hash-code generation methods.
3. MD5 hash generator
4. Hash 32 String
5. Hash 64 String
6. MD5 hashing: Encodes a string
7. MD5 String
8. Hash Code BuilderHash Code Builder
9. HashCode generationHashCode generation
10. Get hash code for primitive data types
11. Return as hash code for the given object
12. Null Safe Hash Code
13. A very efficient java hash algorithm, based on the BuzHash algoritm
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.