Solved

# Calculating hash of an object

Posted on 2011-10-03
561 Views
I have the following function that calculates the has of on object.  There are four integer member variables and 2 String variables. The two member variables have been changed to integers now. so, how would I calculate the hash now.  Is it simply doing the following is enough.  Why string is handled especially.  I am not familiar with how this works, can someone explain it.  Thank you very much.

hash = hash * 17 + intMember1;
hash = hash * 17 + intMember2;
hash = hash * 17 + intMember3;
hash = hash * 17 + intMember4;
hash = hash * 17 +strMember1; <-  This is integer now
hash = hash * 17 + strMember2;<-  This is integer now

The code is actually in AS3.  But, I modified here.
``````public int hashValueOf()
{
int  hash = 1;
hash = hash * 17 + intMember1;
hash = hash * 17 + intMember2;
hash = hash * 17 + intMember3;
hash = hash * 17 + intMember4;
hash = hash * 31 + calculateHash(strMember1);
hash = hash * 31 + calculateHash(strMember2);

return hash;
}

public int calculateHash(String input)
{
int h = 0;
int len = input.length;
for (var i:int = 0; i < len; i++)
{
h = 31 * h + input.charCodeAt(i);
}
return h;
}
``````
0
Question by:ambuli

LVL 47

Expert Comment

I'm not sure I understand what is your question.
Well, you canniot just add the value of the String to your integer number - so ther had to
handle it diefferently (sa to add its hascode).

when you move to int you can treat them as all other int fields
0

LVL 47

Accepted Solution

in the API for Object class
the requirements fo hashCode() method are formulated:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
0

LVL 47

Assisted Solution

So it is required that the same objects produce the same hashCode(), but it is not required that two different objects shopuld
return different  hashCode() - so the sum of all elements would be a good solution
If you are summing up all elements I guess you need to replace string by some int value.
So the hashCode() method of the String itself would be suitable choice

When you move to int's you don't need to use any hashCode()  method - just add those new fields to the sum
0

## Featured Post

### Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.