views:

124

answers:

5

The hashCode of a java Hashtable element is always unique?

If not, how can I guarantee that one search will give me the right element?

+5  A: 

Not necessarily. Two distinct (and not-equal) objects can have the same hashcode.

Anon.
To answer the latter part of your question, HashTable doesn't use HashCode to decide equality. It uses Equals to decide what to return. It uses the HashCode to narrow things down to a handful of elements to test against Equals.
Anon.
A: 

Ideally, yes. In reality, collisions do occasionally happen.

Ignacio Vazquez-Abrams
+1  A: 

From the Java documentation:

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.)

So yes, you can typically expect the default hashCode for an Object to be unique. However, if the method has been overridden by the class you are storing in the Hashtable, all bets are off.

Chinmay Kanchi
A: 

The hashCode of a java Hashtable element is always unique?

They should. At least within the same class.

If not, how can I guarantee that one search will give me the right element?

By specifying your self a good hasCode implementation for your class: Override equals() and hashCode

OscarRyz
+1  A: 

First thing first.
You should consider to use HashMap instead of Hashtable, as the latter is considered obsolete (it enforces implicit synchronization, which is not required most of the time. If you need a synchronized HashMap, it is easily doable)

Now, regarding your question.
Hashcode is not guaranteed to be unique mathematically-wise,
however, when you're using HashMap (or Hashtable), it does not matter.
If two keys generate the same hash code, an equals is automatically invoked on each one of the keys to guarantee that the correct object will be retrieved.

If you're using a String as your key, you're worry free,

But if you're using your own object as the key, you should override the equals and the hashCode methods.
The equals method is mandatory for the proper operation of HashMap, whereas the hashCode method should be coded such that the hash-table will be relatively sparse (otherwise your hashmap, will be just a long array)
If you're using Eclipse there's an easy way to generate hashCode and equals, it basically does all the work for you.

Sug
Are you sure that Eclipse can automatically generate hashCode and equals ? I doubt that, but may be you can prove me wrong by adding some more explanation about it. thanks.
sateesh
Right click on the editor, select source from the context menu -> generate hashCode() and equals()..
Sug
Now I get it, I had never used it. thanks for your your input.
sateesh