-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignHashMap706.java
More file actions
203 lines (195 loc) · 5.66 KB
/
Copy pathDesignHashMap706.java
File metadata and controls
203 lines (195 loc) · 5.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
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
import java.util.Arrays;
/*
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
*/
public class DesignHashMap706 {
public static void main(String[] args) {
MyHashMap obj = new MyHashMap();
obj.put(1, 123);
int param_2 = obj.get(1);
System.out.println(param_2);
obj.remove(1);
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
}
}
class MyHashMap {
final int[] data;
public MyHashMap() {
data = new int[1000001];
Arrays.fill(data, -1);
}
public void put(int key, int value) {
data[key] = value;
}
public int get(int key) {
return data[key];
}
public void remove(int key) {
data[key] = -1;
}
}
//class MyHashMap {
// private static final int CAPACITY = 1000001;
//
// private final List<Entry>[] table;
//
// public MyHashMap() {
// table = new ArrayList[CAPACITY];
// for (int i = 0; i < CAPACITY; i++) {
// table[i] = new ArrayList<>();
// }
// }
//
// public void put(int key, int value) {
// int index = key % CAPACITY;
// for (Entry entry : table[index]) {
// if (entry.key == key) {
// entry.value = value;
// return;
// }
// }
// table[index].add(new Entry(key, value));
// }
//
// public int get(int key) {
// int index = key % CAPACITY;
// for (Entry entry : table[index]) {
// if (entry.key == key) {
// return entry.value;
// }
// }
// return -1;
// }
//
// public void remove(int key) {
// int index = key % CAPACITY;
// Iterator<Entry> iterator = table[index].iterator();
// while (iterator.hasNext()) {
// Entry entry = iterator.next();
// if (entry.key == key) {
// iterator.remove();
// return;
// }
// }
// }
//
// private static class Entry {
// int key;
// int value;
//
// public Entry(int key, int value) {
// this.key = key;
// this.value = value;
// }
// }
//}
//class MyHashMap<K, V> {
// private static final int DEFAULT_CAPACITY = 16;
// private static final double LOAD_FACTOR = 0.75;
//
// private LinkedList<Entry<K, V>>[] buckets;
// private int size;
//
// public MyHashMap() {
// buckets = new LinkedList[DEFAULT_CAPACITY];
// size = 0;
// }
//
// public void put(K key, V value) {
// if (key == null)
// return;
//
// int index = hash(key);
// if (buckets[index] == null) {
// buckets[index] = new LinkedList<>();
// }
//
// for (Entry<K, V> entry : buckets[index]) {
// if (entry.key.equals(key)) {
// entry.value = value;
// return;
// }
// }
//
// buckets[index].add(new Entry<>(key, value));
// size++;
//
// if ((double) size / buckets.length >= LOAD_FACTOR) {
// resize();
// }
// }
//
// public int get(K key) {
// if (key == null)
// return -1;
//
// int index = hash(key);
// if (buckets[index] == null)
// return -1;
//
// for (Entry<K, V> entry : buckets[index]) {
// if (entry.key.equals(key)) {
// return (int) entry.value;
// }
// }
//
// return -1;
// }
//
// public void remove(K key) {
// if (key == null)
// return;
//
// int index = hash(key);
// if (buckets[index] == null)
// return;
//
// buckets[index].removeIf(entry -> entry.key.equals(key));
// }
//
// private int hash(K key) {
// return key.hashCode() % buckets.length;
// }
//
// private void resize() {
// int newCapacity = buckets.length * 2;
// LinkedList<Entry<K, V>>[] newBuckets = new LinkedList[newCapacity];
//
// for (LinkedList<Entry<K, V>> bucket : buckets) {
// if (bucket != null) {
// for (Entry<K, V> entry : bucket) {
// int index = entry.key.hashCode() % newCapacity;
// if (newBuckets[index] == null) {
// newBuckets[index] = new LinkedList<>();
// }
// newBuckets[index].add(entry);
// }
// }
// }
//
// buckets = newBuckets;
// }
//
// private static class Entry<K, V> {
// K key;
// V value;
//
// Entry(K key, V value) {
// this.key = key;
// this.value = value;
// }
// }
//}