Implementing Hash Tables
Implementing a hash table involves designing the data structure and corresponding operations to efficiently store and retrieve key-value pairs. The key idea behind hash tables is to use a hash function that maps keys to an array index, providing constant-time access to values for most operations.
Here is a step-by-step guide to implementing a hash table in Java:
Create a
HashTable
class that will serve as the data structure to store key-value pairs.Declare an inner class
Entry
to represent each key-value pair. Each entry will contain the key, value, and a reference to the next entry (if any) with the same hash code.Define a hash function that takes the key and returns the corresponding index in the array. You can use the built-in
hashCode()
function and apply the modulus operator to ensure it falls within the array bounds.Initialize an array of type
Entry
with a fixed size to hold the key-value pairs. Each index in the array will represent a hash bucket.Implement the
put()
method to insert a new key-value pair into the hash table. This involves calculating the hash code, finding the corresponding bucket, and adding the new entry. If there is a collision, i.e., multiple entries with the same hash code, handle it by appending the entry to the last entry in the bucket's linked list.Implement the
get()
method to retrieve the value associated with a given key. Similar toput()
, calculate the hash code, locate the bucket, and iterate through its linked list until the key is found. Return the corresponding value if found, ornull
otherwise.Implement the
remove()
method to delete a key-value pair from the hash table. Calculate the hash code, locate the bucket, and find the entry with the specified key. Update the references to remove the entry from the linked list, and return the corresponding value. If the key is not found, returnnull
.Optionally, implement the
display()
method to print all the key-value pairs in the hash table. Iterate through each index in the array, and for each non-null entry, iterate through its linked list to display each key-value pair.Finally, write a
main()
method to test the functionalities of the hash table. Create a new instance, insert some key-value pairs, retrieve and remove entries, and display the remaining key-value pairs.
1import java.util.Arrays;
2
3public class HashTable {
4 private static final int SIZE = 10;
5 private Entry[] table;
6
7 private static class Entry {
8 String key;
9 String value;
10 Entry next;
11
12 Entry(String key, String value) {
13 this.key = key;
14 this.value = value;
15 this.next = null;
16 }
17 }
18
19 public HashTable() {
20 table = new Entry[SIZE];
21 }
22
23 private int hash(String key) {
24 return Math.abs(key.hashCode() % SIZE);
25 }
26
27 public void put(String key, String value) {
28 int index = hash(key);
29 if (table[index] == null) {
30 table[index] = new Entry(key, value);
31 } else {
32 Entry entry = table[index];
33 while (entry.next != null) {
34 entry = entry.next;
35 }
36 entry.next = new Entry(key, value);
37 }
38 }
39
40 public String get(String key) {
41 int index = hash(key);
42 Entry entry = table[index];
43 while (entry != null) {
44 if (entry.key.equals(key)) {
45 return entry.value;
46 }
47 entry = entry.next;
48 }
49 return null;
50 }
51
52 public String remove(String key) {
53 int index = hash(key);
54 Entry entry = table[index];
55 Entry prev = null;
56 while (entry != null) {
57 if (entry.key.equals(key)) {
58 if (prev == null) {
59 table[index] = entry.next;
60 } else {
61 prev.next = entry.next;
62 }
63 return entry.value;
64 }
65 prev = entry;
66 entry = entry.next;
67 }
68 return null;
69 }
70
71 public void display() {
72 for (int i = 0; i < SIZE; i++) {
73 if (table[i] != null) {
74 Entry entry = table[i];
75 while (entry != null) {
76 System.out.println("Key: " + entry.key + ", Value: " + entry.value);
77 entry = entry.next;
78 }
79 }
80 }
81 }
82
83 public static void main(String[] args) {
84 HashTable hashMap = new HashTable();
85 hashMap.put("Alice", "555-1234");
86 hashMap.put("Bob", "555-5678");
87 System.out.println("Alice: " + hashMap.get("Alice"));
88 hashMap.remove("Bob");
89 hashMap.display();
90 }
91}
xxxxxxxxxx
}
import java.util.Arrays;
public class HashTable {
private static final int SIZE = 10;
private Entry[] table;
private static class Entry {
String key;
String value;
Entry next;
Entry(String key, String value) {
this.key = key;
this.value = value;
this.next = null;
}
}
public HashTable() {
table = new Entry[SIZE];
}
private int hash(String key) {
return Math.abs(key.hashCode() % SIZE);
}
public void put(String key, String value) {
int index = hash(key);
if (table[index] == null) {