-
Notifications
You must be signed in to change notification settings - Fork 65
/
CustomHashMap.java
495 lines (451 loc) · 11.3 KB
/
CustomHashMap.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* Data-Structures-In-Java
* CustomHashMap.java
*/
package com.deepak.data.structures.Hashing;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Custom HashMap implementation
* Please go through this video for better understanding
* https://www.youtube.com/watch?v=c3RVW3KGIIE
*
* TODO : Implementation of EntrySet and KeySet pending
* @author Deepak
*
* @param <K>
* @param <V>
*/
public class CustomHashMap<K, V> {
/**
* Main method to test the HashMap
* @param args
*/
public static void main(String[] args) {
CustomHashMap<String, Integer> map = new CustomHashMap<>();
System.out.println("Creating a new Map, Size = " + map.size());
System.out.println("Is Map Empty? => " + map.isEmpty());
System.out.println("Adding a new Entry to Map.");
map.put("Deepak Malik", 10);
System.out.println("Is Map Empty? => " + map.isEmpty());
System.out.println("Size of Map => " + map.size());
System.out.println("Does Map contains Key (Deepak Malik)? => " + map.containsKey("Deepak Malik"));
System.out.println("Does Map contains Key (Deepak)? => " + map.containsKey("Deepak"));
System.out.println("Does Map contains Value (10)? => " + map.containsValue(10));
System.out.println("Get the value associated with Key (Deepak Malik)? => " + map.get("Deepak Malik"));
System.out.println("Capacity of Map => " + map.capacity());
System.out.println("Clearing the Map.");
map.clear();
System.out.println("Size of the Map after clearing. => " + map.size());
}
/**
* Initial capacity of HashMap
*/
static final int INITIAL_CAPACITY = 16;
/**
* Maximum capacity of HashMap
*/
static final int MAXIMUM_CAPACITY = 30;
/**
* Default load factor for HashMap
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* HashMap table. This is array of Entry<K, V>
*/
transient Entry<K, V>[] table;
/**
* Entry Set of HashMap
*/
transient Set<Map.Entry<K, V>> entrySet;
/**
* Size of HashMap
*/
transient int size;
/**
* Number of operations done on HashMap
*/
transient int modCount;
/**
* Threshold of HashMap
*/
int threshold;
/**
* Load Factor of HashMap
*/
float loadFactor;
/**
* Constructor based on initial capacity and load factor
* 1. Initial capacity should not be less then 0
* 2. If Initial capacity is greater then Max capacity, then initial capacity will be max capacity
* 3. Load factor should not be less then 0 and should be a number
* 4. Specify load factor and threshold
* 5. Create the table with calculated capacity
*
* @param initialCapacity
* @param loadFactor
*/
@SuppressWarnings("unchecked")
public CustomHashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
}
if (initialCapacity > MAXIMUM_CAPACITY) {
initialCapacity = MAXIMUM_CAPACITY;
}
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
}
this.loadFactor = loadFactor;
threshold = (int) (initialCapacity * loadFactor);
table = new Entry[initialCapacity];
init();
}
/**
* Constructor based on initial capacity
* 1. Create a HashMap based on given capacity and default load factor
*
* @param initialCapacity
*/
public CustomHashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* Default constructor
* 1. Create a HashMap based on default initial capacity and default load factor
*/
@SuppressWarnings("unchecked")
public CustomHashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int) (INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table = new Entry[INITIAL_CAPACITY];
init();
}
/**
* Initialize HashMap
*/
void init() {}
/**
* Method to calculate Hash of a given integer
*
* @param h
* @return {@link int}
*/
private int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
* Method to calculate index where a entry has to be placed in table
*
* @param h
* @param length
* @return {@link int}
*/
private int indexFor(int h, int length) {
return h & (length-1);
}
/**
* Method to calculate size of the HashMap
* @return {@link int}
*/
public int size() {
return size;
}
/**
* Method to check if HashMap is empty
* @return {@link boolean}
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to check Capacity of HashMap
* @return {@link int}
*/
public int capacity() {
return table.length;
}
/**
* Method to clear the content of HashMap
* 1. Create a cloned table
* 2. Loop through each entry and make it null
* 3. Reset the size to initial 0
*/
public void clear() {
modCount++;
Entry<K, V>[] clonedTable = table;
for (int i = 0; i < clonedTable.length; i++) {
clonedTable[i] = null;
}
size = 0;
}
/**
* Method to get the Value from HashMap based on the Key
* 1. If key is null, then value is on 0th index
* 2. Else, check the hash for the given key and find the index
* 3. Loop through all the entries in index
* 4. If hash and key matches, we have found the match
* 5. Return the value
* @param key
* @return {@link V}
*/
public V get(Object key) {
if (key == null) {
return getForNullKey();
} else {
int hash = hash(key.hashCode());
for (Entry<K, V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
return e.value;
}
}
}
return null;
}
/**
* Method to get Value for null key
* 1. Loop through the entries at index 0
* 2. Return the value where 1st key is null
* @return {@link V}
*/
private V getForNullKey() {
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
return e.value;
}
}
return null;
}
/**
* Method to check if HashMap contains the given key
* @param key
* @return {@link boolean}
*/
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
/**
* Method to return the entry associated with given key
* @param key
* @return {@link Entry<K, V>}
*/
final Entry<K, V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
for (Entry<K, V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) {
return e;
}
}
return null;
}
/**
* Method to check if HashMap contains given value
* @param value
* @return {@link boolean}
*/
public boolean containsValue(Object value) {
Entry<K, V>[] clonedTable = table;
for (int i = 0; i < clonedTable.length; i++) {
for(Entry<K, V> e = table[i]; e != null; e = e.next) {
if (e.value == null || value.equals(e.value)) {
return true;
}
}
}
return false;
}
/**
* Method to clone the HashMap
*/
@SuppressWarnings("unchecked")
public Object clone() {
CustomHashMap<K, V> result = null;
try {
result = (CustomHashMap<K, V>)super.clone();
} catch (CloneNotSupportedException e) {
}
result.table = new Entry[table.length];
result.entrySet = null;
result.modCount = 0;
result.size = 0;
result.init();
result.putAllForCreate((Map<? extends K, ? extends V>) this);
return result;
}
/**
* Method to put all values while creating a HashMap
* @param m
*/
private void putAllForCreate(Map<? extends K, ? extends V> m) {
for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
int hash = (entry.getKey() == null) ? 0 : hash(entry.getKey().hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == entry.getKey() || (entry.getKey() != null && entry.getKey().equals(k)))) {
e.value = entry.getValue();
return;
}
}
createEntry(hash, entry.getKey(), entry.getValue(), i);
}
}
/**
* Method to insert a key value pair in HashMap
* @param key
* @param value
* @return {@link V}
*/
public V put(K key, V value) {
if (key == null) {
return putForNullKey(value);
} else {
int hash = hash(key.hashCode());
int index = indexFor(hash, table.length);
for (Entry<K, V> e = table[index]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, index);
}
return null;
}
/**
* Method to insert a value for null key
* @param value
* @return {@link V}
*/
private V putForNullKey(V value) {
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
/**
* Method to add a entry to HashMap at any given index
* @param hash
* @param key
* @param value
* @param bucketIndex
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K, V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K, V>(hash, key, value, e);
if (size++ > threshold) {
resize(2 * table.length);
}
}
/**
* Method to create a entry in HashMap at any given index
* @param hash
* @param key
* @param value
* @param bucketIndex
*/
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K, V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K, V>(hash, key, value, e);
size++;
}
/**
* Method to resize the current HashMap
* @param newCapacity
*/
@SuppressWarnings("unchecked")
void resize(int newCapacity) {
Entry<K, V>[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry<K, V>[] newTable = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int) (newCapacity * loadFactor);
}
/**
* Method to transfer the contents of a HashMap
* @param newTable
*/
void transfer(Entry<K, V>[] newTable) {
Entry<K, V>[] src = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) {
Entry<K, V> e = src[j];
if (e != null) {
src[j] = null;
do {
Entry<K, V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
}
/**
* Static class Entry [This is a Node representation for LinkedList]
* @author Deepak
*
* @param <K>
* @param <V>
*/
static class Entry<K, V> {
final int hash;
final K key;
V value;
Entry<K, V> next;
/**
* Constructor for creating a Entry
* @param hash
* @param key
* @param value
* @param next
*/
public Entry(int hash, K key, V value, Entry<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
/**
* HashCode implementation for entry
*/
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
/**
* Method to check if two objects are equal
*/
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
}