-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashtableMap.java
342 lines (292 loc) · 9.28 KB
/
HashtableMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// --== CS400 Fall 2023 File Header Information ==--
// Name: Cole Movsessian
// Email: [email protected]
// Group: G27
// TA: Grant Waldow
// Lecturer: Florian
// Notes to Grader: <optional extra notes>
import org.junit.Test;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.Assertions;
import java.lang.Math.*;
/**
* Stored key, value pairs in a map
* @param <KeyType> Key type
* @param <ValueType> Value type
*/
public class HashtableMap<KeyType, ValueType> implements MapADT{
/**
* Stores key and value in pair object
*/
protected class Pair {
public KeyType key;
public ValueType value;
public Pair(KeyType key, ValueType value){
this.key = key;
this.value = value;
}
}
protected LinkedList<Pair>[] table;
protected int tableCapacity = 30;
/**
* Default constructor for HashmapTable
* @param capacity
*/
@SuppressWarnings("unchecked")
public HashtableMap(int capacity){
this.tableCapacity = capacity;
table = new LinkedList[capacity];
}
/**
* Constructor to set table to custom capacity
*/
@SuppressWarnings("unchecked")
public HashtableMap(){
table = new LinkedList[tableCapacity];
}
/**
* Adds a new key,value pair/mapping to this collection.
* @param key the key of the key,value pair
* @param value the value that key maps to
* @throws IllegalArgumentException if key already maps to a value
*/
@Override
public void put(Object key, Object value) throws IllegalArgumentException {
// Checks for null or already existing key
if(key == null || containsKey(key)){
throw new IllegalArgumentException("Key is null or already stored");
}
Pair putPair = new Pair((KeyType) key, (ValueType) value);
int keyHash = Math.abs(((key.hashCode()) % tableCapacity)); // calculates hash code to determine table placement
if(table[keyHash] == null){
table[keyHash] = new LinkedList<>();
}
table[keyHash].add(putPair);
// Checks if table capacity needs to be doubled
if(((double)getSize()/(double)getCapacity()) >= 0.75){
doubleHashtable();
}
}
/**
* Doubles the capacity of the hashtable and rehashes stored values
*/
private void doubleHashtable(){
this.tableCapacity = tableCapacity*2;
LinkedList<Pair>[] newTable = new LinkedList[tableCapacity];
// Visits each pairChain
for(LinkedList<Pair> pairChain : table){
if(pairChain != null){
// Visits each pair and copies it to new table
for(Pair pair : pairChain){
// Calculates new hash with doubled table capacity
int newHash = Math.abs((pair.key.hashCode())) % tableCapacity;
if(newTable[newHash] == null){
newTable[newHash] = new LinkedList<>();
}
newTable[newHash].add(pair);
}
}
}
table = newTable;
}
/**
* Checks whether a key maps to a value in this collection.
* @param key the key to check
* @return true if the key maps to a value, and false is the
* key doesn't map to a value
*/
@Override
public boolean containsKey(Object key) {
int keyHash = Math.abs((key.hashCode()) % tableCapacity);
LinkedList<Pair> pairChain = table[keyHash];
if(table[keyHash] == null){
return false;
}
// Visits each pair in pairChain with the key hash
for(Pair pair : pairChain){
if(pair.key.equals(key)){
return true;
}
}
return false;
}
/**
* Retrieves the specific value that a key maps to.
* @param key the key to look up
* @return the value that key maps to
* @throws NoSuchElementException when key is not stored in this
* collection
*/
@Override
public Object get(Object key) throws NoSuchElementException {
// Confirms key is stored
if(!(this.containsKey(key))){
throw new NoSuchElementException("Key not stored");
}
int keyHash = Math.abs((key.hashCode()) % tableCapacity);
LinkedList<Pair> pairChain = table[keyHash];
// Visits each pair in pairChain with the key hash
for(Pair pair : pairChain){
if(pair.key.equals(key)){
return pair.value;
}
}
throw new NoSuchElementException("Key not stored");
}
/**
* Remove the mapping for a key from this collection.
* @param key the key whose mapping to remove
* @return the value that the removed key mapped to
* @throws NoSuchElementException when key is not stored in this
* collection
*/
@Override
public Object remove(Object key) throws NoSuchElementException {
// Confirms key is stored
if(!(containsKey(key))){
throw new NoSuchElementException("Key not stored");
}
Pair removePair = null;
Object removeValue = null;
int keyHash = Math.abs((int)key % tableCapacity);
LinkedList<Pair> pairChain = table[keyHash];
// Visits each pair in pairChain
for(Pair pair : pairChain){
// When key is found pair is removed
if(pair.key.equals(key)){
removePair = pair;
removeValue = pair.value;
break;
}
}
// Removes pair
if(removeValue != null){
pairChain.remove(removePair);
}else{
throw new NoSuchElementException("Key not stored");
}
return removeValue;
}
/**
* Removes all key,value pairs from this collection.
*/
@Override
public void clear() {
// Iterates through every pair in every pairChain and clears values
for(LinkedList<Pair> pairChain : table){
if(pairChain != null) {
pairChain.clear();
}
}
}
/**
* Retrieves the number of keys stored in this collection.
* @return the number of keys stored in this collection
*/
@Override
public int getSize() {
int size = 0;
// Iterates through each pairChain in table
for(LinkedList<Pair> pairChain : table){
// For each non-null pairChain iterates through pairs and increments size
if(pairChain != null){
for(Pair pair : pairChain){
size++;
}
}
}
return size;
}
/**
* Retrieves this collection's capacity.
* @return the size of te underlying array for this collection
*/
@Override
public int getCapacity() {
return tableCapacity;
}
/**
* Tests collisions on pair put
*/
@Test
public void testPutCollision(){
HashtableMap<Integer, Integer> testMap = new HashtableMap();
try{
testMap.put(32,100);
testMap.put(62,200);
testMap.put(92,300);
}catch(Exception e){
Assertions.fail(e.getMessage());
}
}
/**
* Tests contains key method
*/
@Test
public void testContainsKey(){
HashtableMap<Integer, Integer> testMap = new HashtableMap(10);
testMap.put(12,100);
testMap.put(2,100);
Assertions.assertTrue(testMap.containsKey(2));
Assertions.assertTrue(!(testMap.containsKey(3)));
}
/**
* Tests remove method
*/
@Test
public void testRemove(){
HashtableMap<Integer, Integer> testMap = new HashtableMap(10);
testMap.put(10, 99);
testMap.put(12,200);
testMap.put(2,100);
testMap.put(32,300);
testMap.put(3, 45);
testMap.put(9, 69);
testMap.remove(12);
testMap.remove(9);
Assertions.assertTrue(!testMap.containsKey(12) && !testMap.containsKey(9));
Assertions.assertEquals(4, testMap.getSize());
}
/**
* Tests get method
*/
@Test
public void testGet(){
HashtableMap<Integer, Integer> testMap = new HashtableMap(10);
testMap.put(12,200);
testMap.put(2,100);
testMap.put(32,300);
Assertions.assertEquals(100, testMap.get(2));
}
/**
* Tests get size and capacity methods
*/
@Test
public void testGetSizeAndCapacity(){
HashtableMap<Integer, Integer> testMap = new HashtableMap(10);
testMap.put(10, 99);
testMap.put(12,200);
testMap.put(2,100);
testMap.put(32,300);
testMap.put(3, 45);
testMap.put(9, 69);
Assertions.assertEquals(10, testMap.getCapacity());
Assertions.assertEquals(6, testMap.getSize());
}
/**
* Tests clear method
*/
@Test
public void testClear(){
HashtableMap<Integer, Integer> testMap = new HashtableMap(10);
testMap.put(10, 99);
testMap.put(12,200);
testMap.put(2,100);
testMap.put(32,300);
testMap.put(3, 45);
testMap.put(9, 69);
testMap.clear();
Assertions.assertEquals(0, testMap.getSize());
}
}