1

I have to implement a hash table which will use an array, however have to follow a guide and create functions for each procedure. I would appreciate if anyone could help out in completing this for me as I am having some trouble.

public class HashTable {

// public for testing purposes
public int buckets[];

public HashTable(long _a, long _c, long _m) {
}

public void insert(int key) {
}

}

What I've got so far:

public class HashTable {

// public for testing purposes
public int buckets[];

public HashTable(long _a, long _c, long _m) {
    table = new Node[];
    
}

public void insert(int key) {
    Node<T> newNode = new Node(key);
    int posPosition = calPosition(key);
    
}

I have included what I have done so far. Maybe I'm going about it the wrong way. I understand the concept but cannot seem to write code for the hash table so far. Would appreciate any help, Thanks Again

2
  • 1
    SO is not a place for solving homework. We could help you correct your errors, but what effort have you put so far? Commented Jul 2, 2021 at 20:08
  • @Boug I have included what I have done so far. Maybe I'm going about it the wrong way. I understand the concept but cannot seem to write code for the hash table so far. Would appreciate any help, Thanks Again
    – Acire
    Commented Jul 2, 2021 at 20:12

1 Answer 1

5
  • A hash table is simply a list or array of buckets.
  • Each bucket holds all items the key that hashes to for that particular bucket.
  • those items are entries that contain the key and the value you are seeking.

When putting something in a hash table, you use the key/value pair. If the buckets are in an array, use the hash code of the key to get the proper index of the array. If you use a linked list you might have to count to the location.

Then use another array or linked list to store then entry at that cell. Linked lists are better, imo, because they can be added without worry of exceeding the size of the array. They can just be added to the front like a regular linked list.

When adding a value, create the entry, hash the key and find the bucket. Then add the entry to the bucket.

When retrieving a value, hash the key, get to the bucket and do a linear search on the bucket to find the entry for the key you are looking for. Then return the value for that entry.

Note: As with most hash tables, you cannot have duplicate keys. And any Object which is used as a key must override equals and hashCode for this to work.

3
  • Thank you for the clarification, that has helped immensely. I am confused as why there are 3 parameters a,c,m - would they be to represent the key, value and my hashmap?
    – Acire
    Commented Jul 3, 2021 at 12:35
  • 2
    I'm not certain what you mean by a,c,m. Let's say the key is some string in a variable called key. You find the hashcode by doing int hc = key.hashCode();. Let's say you have an array of size 100 to hold the buckets. You could get the index by doing int index = hc%100 which gives you a remainder between 0 and 99 inclusive. You then create an entry (think class) of key and value and store the entry in the bucket at buckets[index]. To retrieve the value you reverse the process. That is just one way to do it. Also note that all objects inherit the hashCode() method from Object.
    – WJS
    Commented Jul 3, 2021 at 13:37
  • 1
    part 2. And classes like Stringoverride hashCode and equals so that is already done for you (and in most cases by classes (e.g Integer and Double) in the API). Does that help?
    – WJS
    Commented Jul 3, 2021 at 13:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.