• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash Code Implementation  RSS feed

 
Nrapendra Sharma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I need to implement hash code generation in my classe.
What are the best practices for doing this ?
I tried Apache lang HashCodeBuilder Class but i am getting different hashCode for same data(values of members of my classfor which i m generating hashCode.

Hoping for a reply soon

Thanks,
Nrapendra
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I've never heard of that class returning a different hash code for the same values before. Can you post the class/fields/values that you are trying to Hash?
 
Nrapendra Sharma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two xml files having same data in it.
Using both xml i m creating object(say o1 and o2) of my class (say Class A)
inside class A i have override the hashCode() method

int hashCode(){
HashCodeBuilder.reflectionHashCode(this, false);
}

members which i dont want to add in hash code generation i have made them transient.

Now when i m comparing hashCode of o1 and o2 ,i m getting different hashCode
 
Neha Kathuriaa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I could not understand your problem .Can you give your code here.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling hashCode() on the HashCodeBuilder? If so, you shouldn't - it returns the hash code of that object, not the created hash code. There is another method (toHashCode() I believe) for that.
 
Neha Kathuriaa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I tried your problem and got the hash codes same for both the objects .Check it out.


I have done it using digester of apache.
[ November 20, 2008: Message edited by: Martijn Verburg ]
 
Neha Kathuriaa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output was
collection.practice.hello@1201a25
digester3=collection.practice.hello@94948a
k =23273
df = 23273
hash codes are equasl
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!