File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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: 24199

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:
subject: HashCode help
It's not a secret anymore!