Could you please explain and give me some examples of its real life usage?
-
You mean `Object.hashCode()`, right? – Romain Linsolas Sep 20 '11 at 07:26
-
There's no such method there. :) – Roman Sep 20 '11 at 07:26
-
you mean `Object.hashCode()` ? – Saket Sep 20 '11 at 07:26
-
for comparing between 2 objects. – Rudy Sep 20 '11 at 07:29
-
[This article](http://www.jchq.net/certkey/0902certkey.htm) will explain the reason why the JVM uses `hashCode()`. Also, it's generally used in Hashed classes/collections like, `HashMap`, `HashSet`, etc. – Buhake Sindi Sep 20 '11 at 07:31
-
take a look to those 2 links: * http://www.cafeaulait.org/course/week4/38.html * http://www.javapractices.com/topic/TopicAction.do?Id=28 – Marco Leo Sep 20 '11 at 07:27
-
Read this - [http://en.wikipedia.org/wiki/Java_hashCode()](http://en.wikipedia.org/wiki/Java_hashCode%28%29) - for a comprehensive explanation. – Saket Sep 20 '11 at 07:28
3 Answers
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.

- 38,762
- 28
- 132
- 190
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.

- 246
- 3
- 14