Each object class in the Java language provides an implementation of the hashCode() method. The output of this method is a 32-bit signed integer and the results are intended to be evenly distributed for varied inputs so as to avoid clustering when used in HashMaps and other structures that store objects in buckets based on their hashCode().
For the java.lang.String class, for instance, a hash code is computed according to the formula:
- s[0] * 31n − 1 + s[1] * 31n − 2 + ... + s[n − 1]
using Java int (32-bit) arithmetic, where s[i] is the ith character of the string, and n is the length of the string.
This is not a cryptographically secure hash algorithm.
Contents |
Usage in Java
The following example shows how a custom object (here named Employee) might implement the public int hashCode() method:
public class Employee{ private int employeeId; private String firstName; private String lastName; private Department dept; /* other methods would be in here somewhere */ public int hashCode() { int hash = 1; hash = hash * 31 + lastName.hashCode(); hash = hash * 29 + firstName.hashCode(); hash = hash * 17 + employeeId; hash = hash * 13 + (dept == null ? 0 : dept.hashCode()); return hash; } }
As with any hash, collisions are possible. For example, the following code:
int hash1 = "ABCDEa123abc".hashCode(); int hash2 = "ABCDFB123abc".hashCode(); System.out.println(hash1==hash2);
Will result in an output of 'true' since both Strings are hashed into the same value.
Sample Implementations of the Algorithm in Various Languages
In Java
NOTE: This is not the implementation of hashCode() in java.lang.String, which is overly complicated for this example. This is an example of how one might implement the algorithm as a static method.
public static int hashCode(String input) { int h = 0; int len = input.length(); for (int i = 0; i < len; i++) { h = 31 * h + input.charAt(i); } return h; }
In Visual Basic for Applications (VBA, the MS office scripting language)
Function hashCode(Value) Const maxInt = 4294967295# Const maxPostInt = 2147483647 Dim h As Currency Dim div As Long h = 0 For i = 1 To Len(Value) h = h * 31 + Asc(Mid(Value, i, 1)) If (h > maxInt) Then div = Int(h / (maxInt + 1)) h = h - (div * (maxInt + 1)) End If Next i If h > maxPostInt Then h = h - maxInt - 1 End If hashCode = h End Function
In ActionScript (Flash)
function hashCode(string) {
var h = 0;
for (var i = 0; i < string.length; i++) {
h = (((31 * h) >> 0) + string.charCodeAt(i)) >> 0;
}
return h;
}
NOTE: The >> operator behaves as described in the ActionScript documentation, which includes conversion of the inputs to 32-bit integers:
"Operator (bitwise); converts expression1 and expression2 to 32-bit integers, and shifts all of the bits in expression1 to the right by the number of places specified by the integer resulting from the conversion of expression2 . Bits that are shifted to the right are discarded. To preserve the sign of the original expression , the bits on the left are filled in with 0 if the most significant bit (the bit farthest to the left) of expression1 is 0, and filled in with 1 if the most significant bit is 1."
References
- Always override hashCode when you override equals in Bloch, Joshua (2008), Effective Java (2 ed.), Addison-Wesley, ISBN 978-0-321-35668-0
No comments have been added.





