Could anyone please let me know the use of hashcode() method in realworld application?
I could know following things from internet about hashcode method.
1. It is the mapping of memory address of an object to an integer value.
2. Hashtable, HashMap and HashSet make use of hashcode() method.
But still my understanding about use of hashcode() method is not clear.
Could anyone please explain or let me know any reference / link available for the exact context of hashcode() method?
Thanks in Advance.
Thanks for the reference link.
I tried the suggested link but it takes me to the Oracle site. But i able to find the content of Chapter 3 of Effective Java.
Do i need to register to Oracle site or is there any direct link available for Chapter 3?
Or click here.
Campbell Ritchie wrote:Is that the old edition? There have been slight changes, but the principles are the same as before.
It's the one listed in our JavaBeginnersFaq.
Jay Shukla wrote:1. It is the mapping of memory address of an object to an integer value.
No, forget about this, it does not have anything to do with the memory address of an object, and it's not important to know this.
The API documentation of the method java.lang.Object.hashCode() says:
API documentation wrote: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.)
But this is just an implementation detail, it is not a requirement, and the implementation might be different in reality. If you override hashCode(), you will typically not do anything with the memory address of the object; it's not even possible to get the memory address of an object in pure Java.
Yes, that was the old edition. It used to be possible to download a PDF of chapter 3 as a sample chapter, but they have changed the link and it now directs to the front page of an Oracle Java Developers website.
Christophe Verré wrote: . . . It's the one listed in our JavaBeginnersFaq.