-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCustomHashMap.java
193 lines (164 loc) · 5.29 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
package by.andd3dfx.collections.custom;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @see <a href="https://youtu.be/-GECqwRV8Uw">Video solution</a>
*/
public class CustomHashMap<K, V> {
private static final int BUCKETS_COUNT = 16;
private CustomLinkedList<CustomEntry<K, V>>[] buckets;
private V valueForNullKey;
private int size = 0;
public CustomHashMap() {
initialize();
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public V get(K key) {
if (key == null) {
return valueForNullKey;
}
int bucketNumber = determineBucketNumber(key);
if (buckets[bucketNumber].isEmpty()) {
return null;
}
for (var curr : buckets[bucketNumber]) {
if (checkEquality(key, curr.getKey())) {
return curr.getValue();
}
}
return null;
}
public V put(K key, V value) {
if (key == null) {
V oldValue = valueForNullKey;
valueForNullKey = value;
return oldValue;
}
int bucketNumber = determineBucketNumber(key);
if (buckets[bucketNumber].isEmpty()) {
buckets[bucketNumber].add(new CustomEntry(key, value));
size++;
return null;
}
for (var curr : buckets[bucketNumber]) {
if (checkEquality(key, curr.getKey())) {
V oldValue = curr.getValue();
curr.setValue(value);
return oldValue;
}
}
buckets[bucketNumber].add(new CustomEntry(key, value));
size++;
return null;
}
public boolean containsKey(K key) {
return get(key) != null;
}
public boolean containsValue(V value) {
for (var bucket : buckets) {
for (var curr : bucket) {
if (checkEquality(value, curr.getValue())) {
return true;
}
}
}
return false;
}
public V remove(K key) {
if (key == null) {
var result = valueForNullKey;
valueForNullKey = null;
size--;
return result;
}
int bucketNumber = determineBucketNumber(key);
for (var curr : buckets[bucketNumber]) {
if (checkEquality(key, curr.getKey())) {
final V result = curr.getValue();
buckets[bucketNumber].remove(curr);
size--;
return result;
}
}
return null;
}
public void clear() {
initialize();
}
public CustomHashSet<K> keySet() {
CustomHashSet<K> result = new CustomHashSet<>();
for (var iterator = keyIterator(); iterator.hasNext(); ) {
result.add(iterator.next());
}
return result;
}
Iterator<K> keyIterator() {
return new KeyIterator<>(buckets);
}
private int determineBucketNumber(K key) {
return Math.abs(key.hashCode() % BUCKETS_COUNT);
}
private class KeyIterator<E> implements Iterator<E> {
private final CustomLinkedList<CustomEntry<E, V>>[] buckets;
private int currentBucketIndex;
private Iterator<CustomEntry<E, V>> currentIterator;
public KeyIterator(CustomLinkedList<CustomEntry<E, V>>[] buckets) {
this.buckets = buckets;
currentBucketIndex = 0;
currentIterator = buckets[currentBucketIndex].iterator();
}
@Override
public boolean hasNext() {
if (currentIterator.hasNext()) {
return true;
}
if (currentBucketIndex < buckets.length - 1) {
currentBucketIndex++;
currentIterator = buckets[currentBucketIndex].iterator();
return hasNext();
}
return false;
}
@Override
public E next() {
if (currentIterator.hasNext()) {
return currentIterator.next().getKey();
}
if (currentBucketIndex < buckets.length - 1) {
currentBucketIndex++;
currentIterator = buckets[currentBucketIndex].iterator();
return next();
}
throw new NoSuchElementException();
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (var bucket : buckets) {
for (var item : bucket) {
sb.append("%s->%s, ".formatted(item.getKey(), item.getValue()));
}
}
return "{" + sb.substring(0, sb.length() - 2) + "}";
}
private boolean checkEquality(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
}
return o1.equals(o2);
}
private void initialize() {
buckets = new CustomLinkedList[BUCKETS_COUNT];
for (var i = 0; i < BUCKETS_COUNT; i++) {
buckets[i] = new CustomLinkedList<>();
}
valueForNullKey = null;
size = 0;
}
}