Skip to content

Commit f088677

Browse files
authored
Merge pull request #1760 from akgmage/dev
add hashtable implementation in java
2 parents 6f1d1cc + 90ec3b7 commit f088677

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

Hash Table/HashTable.java

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
3+
1. **Class Structure:**
4+
- The `HashTable` class represents a hash table for storing key-value pairs.
5+
- It uses a nested `Node` class to encapsulate key-value pairs and handle collisions using a linked list.
6+
- The hash table is initialized with a default size of 7, but this size can be modified as needed.
7+
8+
2. **Node Class:**
9+
- The `Node` class is a private nested class within the `HashTable`.
10+
- Each `Node` contains a key, an associated integer value, and a reference to the next `Node` in case of collisions.
11+
12+
3. **Constructor:**
13+
- The `HashTable` constructor initializes the data map, an array of `Node` objects, with the default size.
14+
15+
4. **`printTable` Method:**
16+
- `printTable` is used to display the contents of the hash table.
17+
- It iterates through the data map and prints the key-value pairs for each index.
18+
- If there are collisions, it prints all elements using the linked list structure.
19+
20+
5. **`hash` Method:**
21+
- `hash` is a helper method to map a given key to an index in the data map.
22+
- It computes a hash code for the key by summing the ASCII values of its characters and applying a modulo operation based on the data map size.
23+
24+
6. **`set` Method:**
25+
- The `set` method adds a new key-value pair to the hash table.
26+
- It computes the index for the given key using the `hash` method and creates a new `Node` for the key-value pair.
27+
- If there's no collision at the computed index, it directly assigns the new `Node`. If a collision occurs, it appends the new `Node` to the linked list.
28+
29+
7. **`get` Method:**
30+
- The `get` method retrieves the value associated with a given key.
31+
- It computes the index for the key using the `hash` method and searches for the key in the linked list at that index.
32+
- If found, it returns the associated value; otherwise, it returns 0.
33+
34+
8. **`keys` Method:**
35+
- The `keys` method returns a list of all keys stored in the hash table.
36+
- It iterates through the data map, collecting all keys, even in the presence of collisions.
37+
38+
In summary, the `HashTable` class provides a basic implementation of a hash table for key-value storage.
39+
It handles collisions through linked lists, allowing multiple key-value pairs to exist at the same index.
40+
The class offers methods to set key-value pairs, retrieve values by keys, list all keys, and print the
41+
contents of the hash table.
42+
*
43+
*/
44+
import java.util.ArrayList;
45+
46+
public class HashTable {
47+
private int size = 7;
48+
private Node[] dataMap;
49+
50+
// Nested Node class to represent key-value pairs
51+
class Node {
52+
String key;
53+
int value;
54+
Node next;
55+
56+
// Constructor to create a new Node
57+
Node(String key, int value) {
58+
this.key = key;
59+
this.value = value;
60+
}
61+
}
62+
63+
// Constructor to create the HashTable with an initial size
64+
public HashTable() {
65+
dataMap = new Node[size];
66+
}
67+
68+
// Method to print the contents of the HashTable
69+
public void printTable() {
70+
for (int i = 0; i < dataMap.length; i++) {
71+
System.out.println(i + ":");
72+
Node temp = dataMap[i];
73+
while (temp != null) {
74+
System.out.println("{" + temp.key + "=" + temp.value + "}");
75+
temp = temp.next;
76+
}
77+
}
78+
}
79+
80+
// A hash function to map a key to an index in the dataMap array
81+
public int hash(String key) {
82+
int hash = 0;
83+
char[] keyChars = key.toCharArray();
84+
for (int i = 0; i < keyChars.length; i++) {
85+
int asciiValue = keyChars[i];
86+
hash = (hash + asciiValue * 23) % dataMap.length;
87+
}
88+
return hash;
89+
}
90+
91+
// Add a key-value pair to the hash table
92+
public void set(String key, int value) {
93+
int index = hash(key);
94+
Node newNode = new Node(key, value);
95+
if (dataMap[index] == null) {
96+
dataMap[index] = newNode;
97+
} else {
98+
Node temp = dataMap[index];
99+
while (temp.next != null) {
100+
temp = temp.next;
101+
}
102+
temp.next = newNode;
103+
}
104+
}
105+
106+
// Get the value associated with a key
107+
public int get(String key) {
108+
int index = hash(key);
109+
Node temp = dataMap[index];
110+
while (temp != null) {
111+
if (temp.key.equals(key)) { // Use .equals() to compare strings
112+
return temp.value;
113+
}
114+
temp = temp.next;
115+
}
116+
return 0; // Return 0 if the key is not found
117+
}
118+
119+
// Get a list of all keys in the hash table
120+
public ArrayList<String> keys() {
121+
ArrayList<String> allKeys = new ArrayList<>();
122+
for (int i = 0; i < dataMap.length; i++) {
123+
Node temp = dataMap[i];
124+
while (temp != null) {
125+
allKeys.add(temp.key);
126+
temp = temp.next;
127+
}
128+
}
129+
return allKeys;
130+
}
131+
}

0 commit comments

Comments
 (0)