-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyMap.java
More file actions
374 lines (349 loc) · 15.7 KB
/
MyMap.java
File metadata and controls
374 lines (349 loc) · 15.7 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
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
package resourceCode.map;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public interface MyMap<K, V> {
// Query Operations
/**
* Returns the number of key-value mappings in this map. If the map contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of key-value mappings in this map
*/
int size();
/**
* Returns <tt>true</tt> if this map contains no key-value mappings.
*
* @return <tt>true</tt> if this map contains no key-value mappings
*/
boolean isEmpty();
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified key.
* More formally, returns <tt>true</tt> if and only if this map contains a
* mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be at most one
* such mapping.)
*
* @param key
* key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified key
* @throws ClassCastException
* if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException
* if the specified key is null and this map does not permit null
* keys (optional)
*/
boolean containsKey(Object key);
/**
* Returns <tt>true</tt> if this map maps one or more keys to the specified
* value. More formally, returns <tt>true</tt> if and only if this map contains
* at least one mapping to a value <tt>v</tt> such that
* <tt>(value==null ? v==null : value.equals(v))</tt>. This operation will
* probably require time linear in the map size for most implementations of the
* <tt>Map</tt> interface.
*
* @param value
* value whose presence in this map is to be tested
* @return <tt>true</tt> if this map maps one or more keys to the specified
* value
* @throws ClassCastException
* if the value is of an inappropriate type for this map (optional)
* @throws NullPointerException
* if the specified value is null and this map does not permit null
* values (optional)
*/
boolean containsValue(Object value);
/**
* Returns the value to which the specified key is mapped, or {@code null} if
* this map contains no mapping for the key.
*
* <p>
* More formally, if this map contains a mapping from a key {@code k} to a value
* {@code v} such that {@code (key==null ? k==null : key.equals(k))}, then this
* method returns {@code v}; otherwise it returns {@code null}. (There can be at
* most one such mapping.)
*
* <p>
* If this map permits null values, then a return value of {@code null} does not
* <i>necessarily</i> indicate that the map contains no mapping for the key;
* it's also possible that the map explicitly maps the key to {@code
* null}. The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @param key
* the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or {@code null} if
* this map contains no mapping for the key
* @throws ClassCastException
* if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException
* if the specified key is null and this map does not permit null
* keys (optional)
*/
V get(Object key);
// Modification Operations
/**
* Associates the specified value with the specified key in this map (optional
* operation). If the map previously contained a mapping for the key, the old
* value is replaced by the specified value. (A map <tt>m</tt> is said to
* contain a mapping for a key <tt>k</tt> if and only if
* {@link #containsKey(Object) m.containsKey(k)} would return <tt>true</tt> .)
*
* @param key
* key with which the specified value is to be associated
* @param value
* value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if
* there was no mapping for <tt>key</tt>. (A <tt>null</tt> return can
* also indicate that the map previously associated <tt>null</tt> with
* <tt>key</tt>, if the implementation supports <tt>null</tt> values.)
* @throws UnsupportedOperationException
* if the <tt>put</tt> operation is not supported by this map
* @throws ClassCastException
* if the class of the specified key or value prevents it from being
* stored in this map
* @throws NullPointerException
* if the specified key or value is null and this map does not
* permit null keys or values
* @throws IllegalArgumentException
* if some property of the specified key or value prevents it from
* being stored in this map
*/
V put(K key, V value);
/**
* Removes the mapping for a key from this map if it is present (optional
* operation). More formally, if this map contains a mapping from key <tt>k</tt>
* to value <tt>v</tt> such that
* <code>(key==null ? k==null : key.equals(k))</code>, that mapping is removed.
* (The map can contain at most one such mapping.)
*
* <p>
* Returns the value to which this map previously associated the key, or
* <tt>null</tt> if the map contained no mapping for the key.
*
* <p>
* If this map permits null values, then a return value of <tt>null</tt> does
* not <i>necessarily</i> indicate that the map contained no mapping for the
* key; it's also possible that the map explicitly mapped the key to
* <tt>null</tt>.
*
* <p>
* The map will not contain a mapping for the specified key once the call
* returns.
*
* @param key
* key whose mapping is to be removed from the map
* @return the previous value associated with <tt>key</tt>, or <tt>null</tt> if
* there was no mapping for <tt>key</tt>.
* @throws UnsupportedOperationException
* if the <tt>remove</tt> operation is not supported by this map
* @throws ClassCastException
* if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException
* if the specified key is null and this map does not permit null
* keys (optional)
*/
V remove(Object key);
// Bulk Operations
/**
* Copies all of the mappings from the specified map to this map (optional
* operation). The effect of this call is equivalent to that of calling
* {@link #put(Object,Object) put(k, v)} on this map once for each mapping from
* key <tt>k</tt> to value <tt>v</tt> in the specified map. The behavior of this
* operation is undefined if the specified map is modified while the operation
* is in progress.
*
* @param m
* mappings to be stored in this map
* @throws UnsupportedOperationException
* if the <tt>putAll</tt> operation is not supported by this map
* @throws ClassCastException
* if the class of a key or value in the specified map prevents it
* from being stored in this map
* @throws NullPointerException
* if the specified map is null, or if this map does not permit null
* keys or values, and the specified map contains null keys or
* values
* @throws IllegalArgumentException
* if some property of a key or value in the specified map prevents
* it from being stored in this map
*/
void putAll(Map<? extends K, ? extends V> m);
/**
* Removes all of the mappings from this map (optional operation). The map will
* be empty after this call returns.
*
* @throws UnsupportedOperationException
* if the <tt>clear</tt> operation is not supported by this map
*/
void clear();
// Views
/**
* Returns a {@link Set} view of the keys contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own <tt>remove</tt> operation), the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
* <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support the
* <tt>add</tt> or <tt>addAll</tt> operations.
*
* @return a set view of the keys contained in this map
*/
Set<K> keySet();
/**
* Returns a {@link Collection} view of the values contained in this map. The
* collection is backed by the map, so changes to the map are reflected in the
* collection, and vice-versa. If the map is modified while an iteration over
* the collection is in progress (except through the iterator's own
* <tt>remove</tt> operation), the results of the iteration are undefined. The
* collection supports element removal, which removes the corresponding mapping
* from the map, via the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
* <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations. It does
* not support the <tt>add</tt> or <tt>addAll</tt> operations.
*
* @return a collection view of the values contained in this map
*/
Collection<V> values();
/**
* Returns a {@link Set} view of the mappings contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own <tt>remove</tt> operation, or
* through the <tt>setValue</tt> operation on a map entry returned by the
* iterator) the results of the iteration are undefined. The set supports
* element removal, which removes the corresponding mapping from the map, via
* the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt> ,
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support the
* <tt>add</tt> or <tt>addAll</tt> operations.
*
* @return a set view of the mappings contained in this map
*/
Set<MyMap.Entry<K, V>> entrySet();
/**
* A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns a
* collection-view of the map, whose elements are of this class. The <i>only</i>
* way to obtain a reference to a map entry is from the iterator of this
* collection-view. These <tt>Map.Entry</tt> objects are valid <i>only</i> for
* the duration of the iteration; more formally, the behavior of a map entry is
* undefined if the backing map has been modified after the entry was returned
* by the iterator, except through the <tt>setValue</tt> operation on the map
* entry.
*
* @see Map#entrySet()
* @since 1.2
*/
interface Entry<K, V> {
/**
* Returns the key corresponding to this entry.
*
* @return the key corresponding to this entry
* @throws IllegalStateException
* implementations may, but are not required to, throw this
* exception if the entry has been removed from the backing map.
*/
K getKey();
/**
* Returns the value corresponding to this entry. If the mapping has been
* removed from the backing map (by the iterator's <tt>remove</tt> operation),
* the results of this call are undefined.
*
* @return the value corresponding to this entry
* @throws IllegalStateException
* implementations may, but are not required to, throw this
* exception if the entry has been removed from the backing map.
*/
V getValue();
/**
* Replaces the value corresponding to this entry with the specified value
* (optional operation). (Writes through to the map.) The behavior of this call
* is undefined if the mapping has already been removed from the map (by the
* iterator's <tt>remove</tt> operation).
*
* @param value
* new value to be stored in this entry
* @return old value corresponding to the entry
* @throws UnsupportedOperationException
* if the <tt>put</tt> operation is not supported by the backing map
* @throws ClassCastException
* if the class of the specified value prevents it from being stored
* in the backing map
* @throws NullPointerException
* if the backing map does not permit null values, and the specified
* value is null
* @throws IllegalArgumentException
* if some property of this value prevents it from being stored in
* the backing map
* @throws IllegalStateException
* implementations may, but are not required to, throw this
* exception if the entry has been removed from the backing map.
*/
V setValue(V value);
/**
* Compares the specified object with this entry for equality. Returns
* <tt>true</tt> if the given object is also a map entry and the two entries
* represent the same mapping. More formally, two entries <tt>e1</tt> and
* <tt>e2</tt> represent the same mapping if
*
* <pre>
* (e1.getKey() == null ? e2.getKey() == null : e1.getKey().equals(e2.getKey()))
* && (e1.getValue() == null ? e2.getValue() == null : e1.getValue().equals(e2.getValue()))
* </pre>
*
* This ensures that the <tt>equals</tt> method works properly across different
* implementations of the <tt>Map.Entry</tt> interface.
*
* @param o
* object to be compared for equality with this map entry
* @return <tt>true</tt> if the specified object is equal to this map entry
*/
boolean equals(Object o);
/**
* Returns the hash code value for this map entry. The hash code of a map entry
* <tt>e</tt> is defined to be:
*
* <pre>
* (e.getKey() == null ? 0 : e.getKey().hashCode()) ˆ (e.getValue() == null ? 0 : e.getValue().hashCode())
* </pre>
*
* This ensures that <tt>e1.equals(e2)</tt> implies that
* <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries <tt>e1</tt> and
* <tt>e2</tt>, as required by the general contract of <tt>Object.hashCode</tt>.
*
* @return the hash code value for this map entry
* @see Object#hashCode()
* @see Object#equals(Object)
* @see #equals(Object)
*/
int hashCode();
}
// Comparison and hashing
/**
* Compares the specified object with this map for equality. Returns
* <tt>true</tt> if the given object is also a map and the two maps represent
* the same mappings. More formally, two maps <tt>m1</tt> and <tt>m2</tt>
* represent the same mappings if <tt>m1.entrySet().equals(m2.entrySet())</tt>.
* This ensures that the <tt>equals</tt> method works properly across different
* implementations of the <tt>Map</tt> interface.
*
* @param o
* object to be compared for equality with this map
* @return <tt>true</tt> if the specified object is equal to this map
*/
boolean equals(Object o);
/**
* Returns the hash code value for this map. The hash code of a map is defined
* to be the sum of the hash codes of each entry in the map's
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> implies
* that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps <tt>m1</tt> and
* <tt>m2</tt>, as required by the general contract of {@link Object#hashCode}.
*
* @return the hash code value for this map
* @see Map.Entry#hashCode()
* @see Object#equals(Object)
* @see #equals(Object)
*/
int hashCode();
}