hash of java hashtable

2019-08-02 19:42发布

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条回答
Anthone
2楼-- · 2019-08-02 19:58

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

查看更多
不美不萌又怎样
3楼-- · 2019-08-02 19:59

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

查看更多
\"骚年 ilove
4楼-- · 2019-08-02 20:00

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.

查看更多
聊天终结者
5楼-- · 2019-08-02 20:16

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.

查看更多
男人必须洒脱
6楼-- · 2019-08-02 20:16

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

查看更多
登录 后发表回答