I am writing a program that will hash words from a document along with their frequency of use and line numbers. I thought that I had finished it when I was told that you have to create a hash table from scratch. I do not know where to begin. Any suggestions as to where and how to start would be appreciated.
Share
A hash table is an important and fundamental data structure. You can read more about it at Wikipedia’s Hash table article. Fortunately, they are fairly easy to implement.
Basically a hash table is a data structure that takes a key and returns or stores a value for that key.
At their core, they are usually implemented using an array, which we shall call
arrsuch that key-value pairs are stored atarr[key.hashCode()%arr.length]. Notice that since your array is not of infinite length, and thathashCodeis not guaranteed to produce unique values you will eventually end up with keys that map to the same index of the array. This is called a collision.One way of resolving these collisions is to store a linked list for each member of
arr. Then the definition ofarrwould look like thisAll objects which map to
arr[key.hashCode()%arr.length]will be added to the list at that position. When you want to retrieve an object from the hashtable, jump to the linked list found atarr[key.hashCode()%arr.length]and iterate through each member of the linked list until you find a key-value pair where the keys are.equals.A good hash table implementation might do things like resize
arronce it gets too full.