I need help on this. Any suggestions?
Hashing is a way of mapping keys to addresses so that there is essentially on searching involved. The idea is to apply a hash function to the unique key value for each data entry to determine where it should go into an array (i.e. which bucket it belongs to). Unfortunately, any useful hash function will have collisions – more than one key being mapped to the same hash value. We will handle collisions by making each bucket the beginning of an ArrayList. For this assignment we will use a very simplistic hash map – mod 5.
You will have at least the following classes:
1. Node – a node will contain an integer for its unique identification number and a String to hold a name. It should be possible to get the name and key value from a node.
2. DataList – This will have an ArrayList to hold nodes that hash to the same value mod 5. The mod operator in Java is %. 7 % 2 would equal 1.This class should have a find method that returns the name belonging to a key or say the key is not on the list. It will also require an add method and a toString method which will nicely create a String which can be printed out using System.out.println();
3. Driver – The driver class will have a loop that constantly shows the user a menu. The user has 4 choices:
a. 1 – Add a new key, name pair (requires creating a Node)
b. 2 – Find the name belonging to a key (key is input name is output)
c. 3 – Display all lists
d. 4 – End
I suggest you create an array of size 5 to hold 5 ArrayLists of Nodes. Use mod to determine where a Node belongs. For example, if your array is called bins then
bins[key %5] would be the correct DataList to put the Node in.