To be specific, suppose that we have an array of objects called
table. This is considered to be a hash table which is
initially empty. We indicate this by using a variable size
whose value is initially 0. Suppose we wish to insert a data
object d into the table. Let the hashing function be
h, so that the hash code for d is
h(d). This is an integer which we assume can be of any size, whereas an
array index must lie between 0 and
table.length-1. We therefore use
h(d) % table.lengthas the index at which to insert the item d. Let us call this index hd, so that the object d is inserted into the table at
table[hd]. The variable size now has the value 1.
So far there is no problem. If we want to check whether d is
present, or if we want to update it, we can recalculate the index
hd and refer to the contents of
table[hd]. Since the table was initially empty, there could not have been any
conflict when we inserted d. Note that we can assume that
the table elements are initialised to null. So it
will be easy to test whether or not a given element of the
table is occupied.
Suppose now we wish to insert a further item, e say. Again we calculate the index
h(e) % table.lengthand call this he. Provided
table[he] is not occupied, we can insert e at this location.
But suppose that, after a number of insertions, we try to insert an item n and find that the location hn defined by
h(n) % table.lengthis already occupied. This means that some earlier item, m say, must already have been inserted for which
h(m) % table.length == h(n) % table.lengthThis could have come about either because m and n had the same hash codes h(m) and h(n) or else because they had the same remainder modulo
table.length.