Calculating hash of an object

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;
}

Open in new window

ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
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
for_yanCommented:
in the API for Object class
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#hashCode%28%29
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.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.