Hi Vini,
Just to add to what Bindu has said...
Say we have three buckets:
Bucket One: will hold all strings of length<3
Bucket Two: will hold strings of length ranging from 4 to 8
Bucket Three: will hold strings of length >8
Then the hashcode() method be doing?
now a get("hi") would look into bucket one and not bother to check others.
So we have basically created a formula for determining which bucket the object should go to, while Storing;
And while retrieving, this formula is used to check which bucket to search for.
If we hadn't done it ourseleves this way, the default int hashCode() generates a unique number for each object...
String s1=new String("language"); // say goes to 123 -
(unique number generated)
and you put it as put(a,"english");
and later try
String s2=new String("language"); // hashes to 345 say -
(unique number generated)
and get(s2);
s2 has got some other hashcode (ie 345) now, so would be looking in bucket 345!
It wouldn't find anything there!
So we have to disable the creation of unique number each time and make it use our custom logic.
Thats what overriding hashcode() does.
HTH,
Vishwa