开发者

equals and hashcode - Justify Contract

If two objects are equal then they should have the same hascode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they a开发者_高级运维re equal) -- Can you please justify it using an example? Thanks


Easy: how many possible strings are there? Now how many possible hash code values are there? How else would you propose handling this problem?

Basically, hash codes are meant to be a way of quickly getting to a possible match, so that you can narrow down the field of candidates extremely quickly. The fact that they don't have to be unique means that they can contain less information than the original data.


To answer the question "How many strings are there?" read Borges' "Library of Babel", (http://jubal.westnet.com/hyperdiscordia/library_of_babel.html).

To answer the question "How many possible Hash Codes are there?" look in your Java documentation for the type returned by Object.hashCode() and then determine how many possible values that type can have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜