It's all about fast lookup. Take any object, how do you look for that object in a container? Go through the container and check each object one by one until you find it? Holy O(n) lookup batman!
Much better would be to sort the objects somehow and at least do binary search. However, some objects cannot be sorted as they don't really have an ordering, and some objects are huge so looking them up is really inefficient that way. What you need is some way to quickly convert any object into a integer, like some kind of identity code, you can then store objects based on that code, and lookups become much much faster. That's the hashcode. It doesn't save your complete headache - 2 objects with the same hashcode aren't necessarily equal, so once you've found all the objects with the hashcode that you're looking for, you still have to check the real object for equality - but hash collisions are rare. At least, with a good hashing algorithm they are ;) Paul Smith [email protected] On Wed, Jun 23, 2010 at 6:07 AM, Rajesh Nair <[email protected]> wrote: > Nataraj, > The hashcode method provides a hash of the object which is primarily used in > hash based containers like HashSet and HashMap. > I do not believe it has anything to do with memory conservation or creation > of duplicate objects. > Regards, > Rajesh Nair > > On Wed, Jun 23, 2010 at 9:24 AM, nataraj subramanian <[email protected]> > wrote: >> >> So hashcodes are used to prevent creation of duplicate objects? It's a >> memory conservation thing? >> >> On Jun 21, 6:50 pm, Paul Smith <[email protected]> wrote: >> > Two object which are equal will have equal hashcodes, but two objects >> > with equal hashcodes aren't necessarily equal. >> > >> > Paul Smith >> > >> > [email protected] >> > >> > On Mon, Jun 21, 2010 at 7:17 AM, Emma Olukha <[email protected]> >> > wrote: >> > > Off the fly..Hash code is an unique id number allocated to an object >> > > by JVM. >> > > It basically identifies each object but is not unique. >> > >> > > </oluka> >> > >> > > On 16 June 2010 14:33, NetBeans <[email protected]> wrote: >> > >> > >> wat is mean by hashcode in java >> > >> > >> -- >> > >> You received this message because you are subscribed to the Google >> > >> Groups >> > >> "google-codejam" group. >> > >> To post to this group, send email to [email protected]. >> > >> To unsubscribe from this group, send email to >> > >> [email protected]. >> > >> For more options, visit this group at >> > >>http://groups.google.com/group/google-code?hl=en. >> > >> > > -- >> > > You received this message because you are subscribed to the Google >> > > Groups >> > > "google-codejam" group. >> > > To post to this group, send email to [email protected]. >> > > To unsubscribe from this group, send email to >> > > [email protected]. >> > > For more options, visit this group at >> > >http://groups.google.com/group/google-code?hl=en. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-codejam" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/google-code?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
