-
Notifications
You must be signed in to change notification settings - Fork 87
/
Custom HashMap Implementation.java
111 lines (92 loc) · 2.89 KB
/
Custom HashMap Implementation.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
import java.util.LinkedList;
class MyHashMap<K, V> {
private static class Entry<K, V> {
K key;
V value;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
private LinkedList<Entry<K, V>>[] table;
private int size;
private static final int INITIAL_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;
@SuppressWarnings("unchecked")
public MyHashMap() {
table = new LinkedList[INITIAL_CAPACITY];
for (int i = 0; i < INITIAL_CAPACITY; i++) {
table[i] = new LinkedList<>();
}
size = 0;
}
private int getIndex(K key) {
return key == null ? 0 : Math.abs(key.hashCode() % table.length);
}
public void put(K key, V value) {
int index = getIndex(key);
LinkedList<Entry<K, V>> bucket = table[index];
for (Entry<K, V> entry : bucket) {
if (entry.key.equals(key)) {
entry.value = value; // Replace existing value
return;
}
}
bucket.add(new Entry<>(key, value));
size++;
if ((float) size / table.length > LOAD_FACTOR) {
resize();
}
}
public V get(K key) {
int index = getIndex(key);
LinkedList<Entry<K, V>> bucket = table[index];
for (Entry<K, V> entry : bucket) {
if (entry.key.equals(key)) {
return entry.value;
}
}
return null; // Not found
}
public void remove(K key) {
int index = getIndex(key);
LinkedList<Entry<K, V>> bucket = table[index];
for (Entry<K, V> entry : bucket) {
if (entry.key.equals(key)) {
bucket.remove(entry);
size--;
return;
}
}
}
public boolean containsKey(K key) {
return get(key) != null;
}
public int size() {
return size;
}
@SuppressWarnings("unchecked")
private void resize() {
LinkedList<Entry<K, V>>[] oldTable = table;
table = new LinkedList[oldTable.length * 2];
for (int i = 0; i < table.length; i++) {
table[i] = new LinkedList<>();
}
size = 0;
for (LinkedList<Entry<K, V>> bucket : oldTable) {
for (Entry<K, V> entry : bucket) {
put(entry.key, entry.value); // Rehash entries into the new table
}
}
}
public static void main(String[] args) {
MyHashMap<String, Integer> map = new MyHashMap<>();
map.put("one", 1);
map.put("two", 2);
System.out.println(map.get("one")); // Should print 1
System.out.println(map.containsKey("two")); // Should print true
map.remove("one");
System.out.println(map.get("one")); // Should print null
System.out.println(map.size()); // Should print 1
}
}