-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
273 lines (214 loc) · 7.03 KB
/
Copy pathHashMap.java
File metadata and controls
273 lines (214 loc) · 7.03 KB
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
package data_structure;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class HashMap<K, V> {
private static final int CAPACITY = 10;
private static final float LOAD_FACTOR = 0.75f;
private Node<K, V>[] hashBucket;
private int currentCapacity;
private int currentSize;
public HashMap() {
this.hashBucket = new Node[CAPACITY];
this.currentSize = 0;
this.currentCapacity = CAPACITY;
}
public void put(K key, V value) {
Node<K, V> node = Node.of(key, value);
int index = getIndex(key);
addNodeToBucket(index, node);
}
public void delete(K key) {
int index = getIndex(key);
Node<K, V> node = findNodeFromKey(index, key);
if (node != null) {
Node<K, V> nextNode = node.getNextNode();
Node<K, V> prevNode = node.getPrevNode();
if (prevNode != null) {
prevNode.setNextNode(nextNode);
}
if (nextNode != null) {
nextNode.setPrevNode(prevNode);
}
if (hashBucket[index] == node) {
hashBucket[index] = nextNode;
if (nextNode == null) {
currentSize--;
}
}
}
}
public V get(K key) {
int index = getIndex(key);
Node<K, V> node = findNodeFromKey(index, key);
if (node == null) {
return null;
}
return node.getValue();
}
public boolean contains(K key) {
int index = getIndex(key);
Node<K, V> node = findNodeFromKey(index, key);
return node != null;
}
public Set<EntrySet<K, V>> entrySet() {
final Set<EntrySet<K, V>> entrySet = new HashSet<>();
for (int i = 0; i < hashBucket.length; i++) {
Node<K, V> kvNode = hashBucket[i];
while (kvNode != null) {
entrySet.add(new EntrySet(kvNode));
kvNode = kvNode.getNextNode();
}
}
return entrySet;
}
private Node<K, V> findNodeFromKey(int index, K key) {
Node<K, V> node = hashBucket[index];
if (node == null) {
return node;
}
while (node != null && !node.getKey().equals(key)) {
node = node.getNextNode();
}
return node;
}
private void addNodeToBucket(int index, Node<K, V> node) {
if (hashBucket[index] == null) {
hashBucket[index] = node;
currentSize++;
double loadFactor = (double) currentSize / currentCapacity;
if (loadFactor > LOAD_FACTOR) {
rearrangeHashBucket(currentCapacity * 2);
}
return;
}
Node<K, V> currentNode = hashBucket[index];
if (currentNode.getKey().equals(node.getKey())) {
currentNode.updateValue(node.getValue());
return;
}
while (currentNode.hasNext()) {
currentNode = currentNode.getNextNode();
if (currentNode.getKey().equals(node.getKey())) {
currentNode.updateValue(node.getValue());
return;
}
}
currentNode.setNextNode(node);
node.setPrevNode(currentNode);
}
private void rearrangeHashBucket(int capacity) {
System.out.println("rearranged hashBucket to : " + capacity);
Node<K, V>[] beforeHashBucket = Arrays.copyOf(hashBucket, hashBucket.length);
this.hashBucket = new Node[capacity];
this.currentCapacity = capacity;
this.currentSize = 0;
for (Node<K, V> beforeNode : beforeHashBucket) {
if (beforeNode == null) {
continue;
}
Node<K, V> current = beforeNode;
do {
K key = current.getKey();
int index = getIndex(key);
addNodeToBucket(index, beforeNode);
} while ((current = current.getNextNode()) != null);
}
}
private int getIndex(K key) {
return Math.abs(key.hashCode() % currentCapacity);
}
/*
* for test
* */
public void print() {
System.out.println("---- info ----");
System.out.println("bucket length : " + hashBucket.length);
System.out.println("capacity : " + currentCapacity);
System.out.println("size : " + currentSize);
System.out.println();
System.out.println("---- data ----");
for (int i = 0; i < hashBucket.length; i++) {
Node<K, V> kvNode = hashBucket[i];
while (kvNode != null) {
System.out.println("index = %s / Key = %s / Value = %s".formatted(i, kvNode.getKey(), kvNode.getValue()));
kvNode = kvNode.getNextNode();
}
}
}
static class Node<K, V> {
private Node<K, V> nextNode;
private Node<K, V> prevNode;
private K key;
private V value;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public static <K, V> Node<K, V> of(K key, V value) {
return new Node<>(key, value);
}
public void updateValue(V value) {
this.value = value;
}
public void setNextNode(Node<K, V> nextNode) {
this.nextNode = nextNode;
}
public void setPrevNode(Node<K, V> prevNode) {
this.prevNode = prevNode;
}
public boolean hasNext() {
return nextNode != null;
}
public Node<K, V> getNextNode() {
return nextNode;
}
public Node<K, V> getPrevNode() {
return prevNode;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
static class EntrySet<K, V> {
private K key;
private V value;
public EntrySet(Node<K, V> node) {
this.key = node.getKey();
this.value = node.getValue();
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
public static void main(String[] args) {
HashMap<String, Integer> stringHashMap = new HashMap<>();
stringHashMap.put("a", 1);
stringHashMap.put("b", 2);
stringHashMap.put("c", 3);
stringHashMap.put("d", 4);
stringHashMap.put("e", 5);
stringHashMap.put("f", 6);
stringHashMap.put("g", 7);
stringHashMap.put("h", 8);
stringHashMap.put("i", 9);
stringHashMap.put("j", 10);
stringHashMap.put("k", 11);
stringHashMap.put("a", 11);
stringHashMap.put("k", 101);
stringHashMap.delete("k");
stringHashMap.delete("a");
stringHashMap.delete("c");
stringHashMap.print();
System.out.println();
System.out.println(stringHashMap.get("j"));
System.out.println(stringHashMap.get("k"));
System.out.println(stringHashMap.get("d"));
}
}