Mark As Completed Discussion

To implement a hash table, we first need to set up a hash table class and then instantiate it in code. Let's start by creating a HashTable class in Java:

TEXT/X-JAVA
1class HashTable<K, V> {
2
3    private static final int SIZE = 10;
4
5    private Entry<K, V>[] buckets;
6
7    public HashTable() {
8        this.buckets = new Entry[SIZE];
9    }
10
11    // Other methods
12}

In the HashTable class, we have defined a generic type K for the key and V for the value. We have also created a fixed-size array buckets to store the key-value pairs.

To insert a key-value pair into the hash table, we need to calculate the index using a hash function. For simplicity, let's use the built-in hashCode() method of the key object and take the modulus of it with the size of the array. Here's the implementation of the insert() method:

TEXT/X-JAVA
1public void insert(K key, V value) {
2    int index = getIndex(key);
3
4    Entry<K, V> newEntry = new Entry<>(key, value);
5
6    if (buckets[index] == null) {
7        buckets[index] = newEntry;
8    } else {
9        Entry<K, V> currentEntry = buckets[index];
10        while (currentEntry.next != null) {
11            if (currentEntry.key.equals(key)) {
12                currentEntry.value = value;
13                return;
14            }
15            currentEntry = currentEntry.next;
16        }
17        currentEntry.next = newEntry;
18    }
19}

The insert() method first calculates the index using the getIndex() helper method. If the bucket at the calculated index is empty, it simply stores the new key-value pair there. Otherwise, it iterates through the linked list at the bucket to find the key. If the key is found, it updates the value. If the key is not found, it adds the new key-value pair at the end of the linked list.

To retrieve a value based on a key from the hash table, we use the get() method. Here's the implementation:

TEXT/X-JAVA
1public V get(K key) {
2    int index = getIndex(key);
3
4    if (buckets[index] == null) {
5        return null;
6    }
7
8    Entry<K, V> currentEntry = buckets[index];
9    while (currentEntry != null) {
10        if (currentEntry.key.equals(key)) {
11            return currentEntry.value;
12        }
13        currentEntry = currentEntry.next;
14    }
15
16    return null;
17}

The get() method first calculates the index using the getIndex() method. If the bucket at the calculated index is empty, it returns null. Otherwise, it iterates through the linked list at the bucket to find the key. If the key is found, it returns the corresponding value. If the key is not found, it returns null.

To delete a key-value pair from the hash table, we use the remove() method. Here's the implementation:

TEXT/X-JAVA
1public void remove(K key) {
2    int index = getIndex(key);
3
4    if (buckets[index] == null) {
5        return;
6    }
7
8    if (buckets[index].key.equals(key)) {
9        buckets[index] = buckets[index].next;
10        return;
11    }
12
13    Entry<K, V> currentEntry = buckets[index];
14    Entry<K, V> prevEntry = null;
15
16    while (currentEntry != null) {
17        if (currentEntry.key.equals(key)) {
18            prevEntry.next = currentEntry.next;
19            return;
20        }
21        prevEntry = currentEntry;
22        currentEntry = currentEntry.next;
23    }
24}

The remove() method first calculates the index using the getIndex() method. If the bucket at the calculated index is empty, it simply returns. If the first entry in the linked list at the bucket matches the key, it updates the bucket to skip the first entry. Otherwise, it iterates through the linked list to find the key. If the key is found, it updates the previous entry's next reference to skip the current entry, effectively removing it. If the key is not found, it simply returns.

Now that we have implemented the basic functionality of a hash table, let's test it out with some example code:

TEXT/X-JAVA
1public static void main(String[] args) {
2    HashTable<String, Integer> hashTable = new HashTable<>();
3
4    hashTable.insert("Alice", 24);
5    hashTable.insert("Bob", 32);
6    hashTable.insert("Charlie", 41);
7
8    int age1 = hashTable.get("Alice");
9    int age2 = hashTable.get("Bob");
10    int age3 = hashTable.get("Charlie");
11
12    System.out.println("Alice's age: " + age1);
13    System.out.println("Bob's age: " + age2);
14    System.out.println("Charlie's age: " + age3);
15
16    hashTable.remove("Bob");
17    System.out.println("Bob's age after removal: " + hashTable.get("Bob"));
18}

The code creates a HashTable instance and inserts three key-value pairs. It then retrieves the ages of Alice, Bob, and Charlie using the get() method and prints them. Finally, it removes the entry for Bob and tries to retrieve his age again, which should return null.

Congratulations! You have implemented a basic hash table that supports insertion, retrieval, and deletion of key-value pairs.

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment