|
A little intro to Hash tables: When inserting an object into a hashtable the hash code is retrieved from that object. Based on the hash code the object is placed in one of several buckets. (the number of buckets varies) The bucket is just a linked list of objects. Now when trying to find an object the hash code is used to locate the bucket and then the equals is called for each object in the bucket to find the exact match. So think of hash code as fuzzy match and equals() as exact match.
One thing you have done here is changed the hash code for an object. That should not be done once the object is placed within the hash table. The reason is you just change what bucket the object should be in without actually moving the object.
Also your equals is comparing the same as your hash code. This will not work well. Remember all objects in the same bucket have the same hash code and you can have multiple objects in a hash table that have the same hash code. The .equals() method is supposed to be able to tell them apart. In some of your examples you set k1 and k2 to have the same i value. Now you cannot tell them apart.
|