开发者

What's the purpose of Java's Object.hashCode() method? [duplicate]

This question already has answers here: What is the use of hashCode in Java? (8 answers) 开发者_如何学运维 Closed 5 years ago.

Could you please explain and give me some examples of its real life usage?


It returns a hashCode for an object which is used in Hashing Based DataStructure (For example : HashMap)

See Also

  • API Doc


Basically if you wish to create a data structure to store objects and have an access time of O(1) you create a hash table. You hash elements of the object into a key which is unique based on the order of characters etc. Once this is done you can access the data quickly. This is usually used for data structures containing strings without a index value being stored with them therefore you wouldn't need to use the "vector" function in c++ or its equivilant in java.


Giving hashtable support for a Java class. Look at the api. An hash function is used to distinguish entities of a same type in a quick way: with hash you don't need to compare all field inside an object every time you are comparing it to another one. Once generating its hash (fingerprint), if you use a hash function that avoids collision, it becomes a quickly method to compare objects because you need only to compare the hash.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜