-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashMap.java
More file actions
136 lines (122 loc) · 3.66 KB
/
MyHashMap.java
File metadata and controls
136 lines (122 loc) · 3.66 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
public class MyHashMap<K, V> {
private static final int DEFAULT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private int capacity;
private float loadFactor;
private int size;
private Node<K, V>[] table;
public MyHashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
@SuppressWarnings("unchecked")
public MyHashMap(int capacity, float loadFactor) {
this.capacity = capacity;
this.loadFactor = loadFactor;
this.table = new Node[capacity];
}
private static class Node<K, V> {
K key;
V value;
Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
public int size() {
return size;
}
private int hash(K key) {
int hashCode = key.hashCode();
int index = hashCode & 0x7fffffff; // Apply bitwise AND with 0x7fffffff to get non-negative value
return index % capacity; // https://mathcenter.oxford.emory.edu/site/cs171/usingTheHashCodeMethod/
}
public void put(K key, V value) {
int index = hash(key);
Node<K, V> node = table[index];
while (node != null) {
if (node.key.equals(key)) {
node.value = value;
return;
}
node = node.next;
}
Node<K, V> newNode = new Node<>(key, value);
newNode.next = table[index];
table[index] = newNode;
size++;
if (size > capacity * loadFactor) {
resize();
}
}
public V get(K key) {
int index = hash(key);
Node<K, V> node = table[index];
while (node != null) {
if (node.key.equals(key)) {
return node.value;
}
node = node.next;
}
return null;
}
public boolean remove(K key) {
int index = hash(key);
Node<K, V> node = table[index];
Node<K, V> prev = null;
while (node != null) {
if (node.key.equals(key)) {
if (prev == null) {
table[index] = node.next;
} else {
prev.next = node.next;
}
size--;
return true;
}
prev = node;
node = node.next;
}
return false;
}
@SuppressWarnings("unchecked")
private void resize() {
int newCapacity = capacity * 2; // doubling capacity
Node<K, V>[] newTable = new Node[newCapacity];
for (int i = 0; i < capacity; i++) {
Node<K, V> node = table[i];
while (node != null) {
Node<K, V> next = node.next;
int index = hash(node.key);
node.next = newTable[index];
newTable[index] = node;
node = next;
}
}
table = newTable;
capacity = newCapacity;
}
public String toString() {
if (size == 0)
return "{ [ ] }";
StringBuilder sb = new StringBuilder();
sb.append("\n{");
for (int i = 0; i < table.length; i++) {
Node<K, V> node = table[i];
Node<K, V> prev = null;
while (node != null) {
if (prev == null) {
sb.append("\n [ ");
}
sb.append(node.key + " : " + node.value);
prev = node;
node = node.next;
if (node == null) {
sb.append(" ]");
}
}
}
sb.append("\n}");
return sb.toString();
}
}