|
It's not actually a table, but rather an array. The hash() method returns a suitably random number (but will always return the same number for the same value). The position in the array is that hash value. As a result, in an infinitely large hash table, there is no searching for values, you just go to it's position and retrieve it. Of course, no hash table is of infinite size, so the question becomes "what if the hash value is outside the length of the array"? You use a modulus operator to loop back around and start from the beginning.
Say you have a hash table of size "10", but your Object has a hash value of "11" - where does it get inserted? At position "1", because 11 % 10 = 1.
Now, what if that spot is already occupied? That is known as a collision. There are a number of different ways of dealing with it, but the simplest is to merely move down the array until you find an empty spot. When someone wants to look up an object in the hash table, you look at what's contained at the spot that it hashes to. If it doesn't match the key of the object you're looking for, continue down the array until you either find it or loop. It's not terribly efficient in the case of collisions, but it's easy to code and I assume that's what you're looking for right now.
|