It's not a secret anymore!*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashCode help" Watch "HashCode help" New topic
Author

HashCode help

Steve Chambers
Greenhorn

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashCode help
 
Similar Threads
HashMaps and Key Selection
Populating HashMaps of HashMap of Objects
hashcode - What is it for?
hashtable working & overriding equals
Overriding hashCode() and equals()