This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashCode help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashCode help" Watch "HashCode help" New topic

HashCode help

Steve Chambers

Joined: Sep 19, 2006
Posts: 28
Hopefully someone can help me with this issue: I have a structure (suffix tree) that is built using hashmaps. In these hashmaps I store objects. Each object has a string field. If anyone is familiar with how a suffix tree works this will make sense.

Basically I store a sequence of objects on the tree. When I add the suffix part to the tree the hashcodes of the object are the same as the hashcode of the orignial object, even though I have instansiated a new object.

Does anyone know why this would be the case? Why am I getting the same hashcode for different objects?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

The "hashCode()" method you inherit from Object gives you a value that never changes in the life of an individual object. If you want hashCode() to return a value based on the fields in an object, then you need to override it and supply your own version that does this.

Does this answer your question?

[Jess in Action][AskingGoodQuestions]
subject: HashCode help
It's not a secret anymore!