This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.java
241 lines (205 loc) · 5.98 KB
/
HashTable.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
package assign09;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* This class creates a HashTable that maps keys to values and uses various
* methods to interact with those keys and values.
*
* @author Nils Streedain & Paul Nuffer
* @version April 6, 2021
*/
public class HashTable<K, V> implements Map<K, V> {
private ArrayList<LinkedList<MapEntry<K, V>>> table;
private int itemCount;
private int collisons;
private final int INITIAL_BACKING_SIZE = 100;
private final double LOAD_FACTOR_CAP = 1.5;
public HashTable() {
table = new ArrayList<LinkedList<MapEntry<K, V>>>();
for (int i = 0; i < INITIAL_BACKING_SIZE; i++)
table.add(new LinkedList<MapEntry<K, V>>());
itemCount = 0;
}
/**
* Removes all mappings from this HashTable.
*
* O(table length)
*/
@Override
public void clear() {
// performs the same tasks as the constructor
itemCount = 0;
table = new ArrayList<LinkedList<MapEntry<K, V>>>();
for (int i = 0; i < INITIAL_BACKING_SIZE; i++)
table.add(new LinkedList<MapEntry<K, V>>());
}
/**
* Determines whether this map contains the specified key.
*
* O(1)
*
* @param key
* @return true if this map contains the key, false otherwise
*/
@Override
public boolean containsKey(K key) {
// gets the LinkedList that the key must reside in, if it exists
LinkedList<MapEntry<K, V>> chain = table.get(Math.abs(key.hashCode() % table.size()));
// checks each entry in the chain for the matching key
for (MapEntry<K, V> currEntry : chain)
if (currEntry.getKey().equals(key))
// exits early if a match is found
return true;
return false;
}
/**
* Determines whether this map contains the specified value.
*
* O(table length)
*
* @param value
* @return true if this map contains one or more keys to the specified value,
* false otherwise
*/
@Override
public boolean containsValue(V value) {
//iterates over every chain in the backing array, as the value
//could be anywhere
for (LinkedList<MapEntry<K, V>> currChain : table)
for (MapEntry<K, V> currEntry : currChain)
if (currEntry.getValue().equals(value))
return true;
return false;
}
/**
* Returns a List view of the mappings contained in this HashTable, where the
* ordering of mapping in the list is insignificant.
*
* O(table length)
*
* @return a List object containing all mapping (i.e., entries) in this
* HashTable
*/
@Override
public List<MapEntry<K, V>> entries() {
List<MapEntry<K, V>> newList = new ArrayList<MapEntry<K, V>>();
//iterates over every item
for (LinkedList<MapEntry<K, V>> chain : table)
for (MapEntry<K, V> currEntry : chain)
newList.add(currEntry);
return newList;
}
/**
* Gets the value to which the specified key is mapped.
*
* O(1)
*
* @param key
* @return the value to which the specified key is mapped, or null if this
* HashTable contains no mapping for the key
*/
@Override
public V get(K key) {
LinkedList<MapEntry<K, V>> chain = table.get(Math.abs(key.hashCode() % table.size()));
//iterates over all items in the chain to find a match
for (MapEntry<K, V> currEntry : chain)
if (currEntry.getKey().equals(key))
return currEntry.getValue();
return null;
}
/**
* Determines whether this HashTable contains any mappings.
*
* O(1)
*
* @return true if this HashTable contains no mappings, false otherwise
*/
@Override
public boolean isEmpty() {
return itemCount == 0;
}
/**
* Associates the specified value with the specified key in this HashTable.
* (I.e., if the key already exists in this HashTable, resets the value;
* otherwise adds the specified key-value pair.)
*
* O(1)
*
* @param key
* @param value
* @return the previous value associated with key, or null if there was no
* mapping for key
*/
@Override
public V put(K key, V value) {
//ensures the load factor does not exceed a limit
//load factor is the average length of all the LinkedLists in the backing array
if (((double) itemCount) / ((double) table.size()) > LOAD_FACTOR_CAP) {
//store all current items
List<MapEntry<K, V>> entries = this.entries();
int newSize = table.size() * 2;
//does what the constructor does, but for a doubled value
table = new ArrayList<LinkedList<MapEntry<K, V>>>();
itemCount = 0;
for (int i = 0; i < newSize; i++)
table.add(new LinkedList<MapEntry<K, V>>());
//uses our put method with all the items we stored earlier
for (MapEntry<K, V> currEntry : entries)
this.put(currEntry.getKey(), currEntry.getValue());
}
LinkedList<MapEntry<K, V>> chain = table.get(Math.abs(key.hashCode() % table.size()));
for (MapEntry<K, V> currEntry : chain) {
//updates the value if a key mapping already exists
if (currEntry.getKey().equals(key)) {
V prevValue = currEntry.getValue();
currEntry.setValue(value);
return prevValue;
}
collisons++;
}
//if no existing mapping, add the new item
itemCount++;
chain.add(new MapEntry<K, V>(key, value));
return null;
}
/**
* Removes the mapping for a key from this HashTable if it is present.
*
* O(1)
*
* @param key
* @return the previous value associated with key, or null if there was no
* mapping for key
*/
@Override
public V remove(K key) {
LinkedList<MapEntry<K, V>> chain = table.get(Math.abs(key.hashCode() % table.size()));
for (MapEntry<K, V> currEntry : chain) {
//if the value is found, store it to return, and remove it
if (currEntry.getKey().equals(key)) {
V prevValue = currEntry.getValue();
itemCount--;
chain.remove(currEntry);
return prevValue;
}
collisons++;
}
return null;
}
/**
* Determines the number of mappings in this HashTable.
*
* O(1)
*
* @return the number of mappings in this HashTable
*/
@Override
public int size() {
return itemCount;
}
//only used for testing purposed, commented out for 'release'
// public int getCollisons() {
// return collisons;
// }
}