Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

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

0
ambuli
Asked:
ambuli
  • 3
2 Solutions
 
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
0
 
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.)
0
 
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now