jaroslav@557
|
1 |
/*
|
jaroslav@557
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@557
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@557
|
4 |
*
|
jaroslav@557
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@557
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@557
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@557
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@557
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@557
|
10 |
*
|
jaroslav@557
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@557
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@557
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@557
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@557
|
15 |
* accompanied this code).
|
jaroslav@557
|
16 |
*
|
jaroslav@557
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@557
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@557
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@557
|
20 |
*
|
jaroslav@557
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@557
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@557
|
23 |
* questions.
|
jaroslav@557
|
24 |
*/
|
jaroslav@557
|
25 |
|
jaroslav@557
|
26 |
package java.util;
|
jaroslav@557
|
27 |
import java.io.*;
|
jaroslav@557
|
28 |
|
jaroslav@557
|
29 |
/**
|
jaroslav@557
|
30 |
* Hash table based implementation of the <tt>Map</tt> interface. This
|
jaroslav@557
|
31 |
* implementation provides all of the optional map operations, and permits
|
jaroslav@557
|
32 |
* <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
|
jaroslav@557
|
33 |
* class is roughly equivalent to <tt>Hashtable</tt>, except that it is
|
jaroslav@557
|
34 |
* unsynchronized and permits nulls.) This class makes no guarantees as to
|
jaroslav@557
|
35 |
* the order of the map; in particular, it does not guarantee that the order
|
jaroslav@557
|
36 |
* will remain constant over time.
|
jaroslav@557
|
37 |
*
|
jaroslav@557
|
38 |
* <p>This implementation provides constant-time performance for the basic
|
jaroslav@557
|
39 |
* operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
|
jaroslav@557
|
40 |
* disperses the elements properly among the buckets. Iteration over
|
jaroslav@557
|
41 |
* collection views requires time proportional to the "capacity" of the
|
jaroslav@557
|
42 |
* <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
|
jaroslav@557
|
43 |
* of key-value mappings). Thus, it's very important not to set the initial
|
jaroslav@557
|
44 |
* capacity too high (or the load factor too low) if iteration performance is
|
jaroslav@557
|
45 |
* important.
|
jaroslav@557
|
46 |
*
|
jaroslav@557
|
47 |
* <p>An instance of <tt>HashMap</tt> has two parameters that affect its
|
jaroslav@557
|
48 |
* performance: <i>initial capacity</i> and <i>load factor</i>. The
|
jaroslav@557
|
49 |
* <i>capacity</i> is the number of buckets in the hash table, and the initial
|
jaroslav@557
|
50 |
* capacity is simply the capacity at the time the hash table is created. The
|
jaroslav@557
|
51 |
* <i>load factor</i> is a measure of how full the hash table is allowed to
|
jaroslav@557
|
52 |
* get before its capacity is automatically increased. When the number of
|
jaroslav@557
|
53 |
* entries in the hash table exceeds the product of the load factor and the
|
jaroslav@557
|
54 |
* current capacity, the hash table is <i>rehashed</i> (that is, internal data
|
jaroslav@557
|
55 |
* structures are rebuilt) so that the hash table has approximately twice the
|
jaroslav@557
|
56 |
* number of buckets.
|
jaroslav@557
|
57 |
*
|
jaroslav@557
|
58 |
* <p>As a general rule, the default load factor (.75) offers a good tradeoff
|
jaroslav@557
|
59 |
* between time and space costs. Higher values decrease the space overhead
|
jaroslav@557
|
60 |
* but increase the lookup cost (reflected in most of the operations of the
|
jaroslav@557
|
61 |
* <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>). The
|
jaroslav@557
|
62 |
* expected number of entries in the map and its load factor should be taken
|
jaroslav@557
|
63 |
* into account when setting its initial capacity, so as to minimize the
|
jaroslav@557
|
64 |
* number of rehash operations. If the initial capacity is greater
|
jaroslav@557
|
65 |
* than the maximum number of entries divided by the load factor, no
|
jaroslav@557
|
66 |
* rehash operations will ever occur.
|
jaroslav@557
|
67 |
*
|
jaroslav@557
|
68 |
* <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
|
jaroslav@557
|
69 |
* creating it with a sufficiently large capacity will allow the mappings to
|
jaroslav@557
|
70 |
* be stored more efficiently than letting it perform automatic rehashing as
|
jaroslav@557
|
71 |
* needed to grow the table.
|
jaroslav@557
|
72 |
*
|
jaroslav@557
|
73 |
* <p><strong>Note that this implementation is not synchronized.</strong>
|
jaroslav@557
|
74 |
* If multiple threads access a hash map concurrently, and at least one of
|
jaroslav@557
|
75 |
* the threads modifies the map structurally, it <i>must</i> be
|
jaroslav@557
|
76 |
* synchronized externally. (A structural modification is any operation
|
jaroslav@557
|
77 |
* that adds or deletes one or more mappings; merely changing the value
|
jaroslav@557
|
78 |
* associated with a key that an instance already contains is not a
|
jaroslav@557
|
79 |
* structural modification.) This is typically accomplished by
|
jaroslav@557
|
80 |
* synchronizing on some object that naturally encapsulates the map.
|
jaroslav@557
|
81 |
*
|
jaroslav@557
|
82 |
* If no such object exists, the map should be "wrapped" using the
|
jaroslav@557
|
83 |
* {@link Collections#synchronizedMap Collections.synchronizedMap}
|
jaroslav@557
|
84 |
* method. This is best done at creation time, to prevent accidental
|
jaroslav@557
|
85 |
* unsynchronized access to the map:<pre>
|
jaroslav@557
|
86 |
* Map m = Collections.synchronizedMap(new HashMap(...));</pre>
|
jaroslav@557
|
87 |
*
|
jaroslav@557
|
88 |
* <p>The iterators returned by all of this class's "collection view methods"
|
jaroslav@557
|
89 |
* are <i>fail-fast</i>: if the map is structurally modified at any time after
|
jaroslav@557
|
90 |
* the iterator is created, in any way except through the iterator's own
|
jaroslav@557
|
91 |
* <tt>remove</tt> method, the iterator will throw a
|
jaroslav@557
|
92 |
* {@link ConcurrentModificationException}. Thus, in the face of concurrent
|
jaroslav@557
|
93 |
* modification, the iterator fails quickly and cleanly, rather than risking
|
jaroslav@557
|
94 |
* arbitrary, non-deterministic behavior at an undetermined time in the
|
jaroslav@557
|
95 |
* future.
|
jaroslav@557
|
96 |
*
|
jaroslav@557
|
97 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
jaroslav@557
|
98 |
* as it is, generally speaking, impossible to make any hard guarantees in the
|
jaroslav@557
|
99 |
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
jaroslav@557
|
100 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
jaroslav@557
|
101 |
* Therefore, it would be wrong to write a program that depended on this
|
jaroslav@557
|
102 |
* exception for its correctness: <i>the fail-fast behavior of iterators
|
jaroslav@557
|
103 |
* should be used only to detect bugs.</i>
|
jaroslav@557
|
104 |
*
|
jaroslav@557
|
105 |
* <p>This class is a member of the
|
jaroslav@557
|
106 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
jaroslav@557
|
107 |
* Java Collections Framework</a>.
|
jaroslav@557
|
108 |
*
|
jaroslav@557
|
109 |
* @param <K> the type of keys maintained by this map
|
jaroslav@557
|
110 |
* @param <V> the type of mapped values
|
jaroslav@557
|
111 |
*
|
jaroslav@557
|
112 |
* @author Doug Lea
|
jaroslav@557
|
113 |
* @author Josh Bloch
|
jaroslav@557
|
114 |
* @author Arthur van Hoff
|
jaroslav@557
|
115 |
* @author Neal Gafter
|
jaroslav@557
|
116 |
* @see Object#hashCode()
|
jaroslav@557
|
117 |
* @see Collection
|
jaroslav@557
|
118 |
* @see Map
|
jaroslav@557
|
119 |
* @see TreeMap
|
jaroslav@557
|
120 |
* @see Hashtable
|
jaroslav@557
|
121 |
* @since 1.2
|
jaroslav@557
|
122 |
*/
|
jaroslav@557
|
123 |
|
jaroslav@557
|
124 |
public class HashMap<K,V>
|
jaroslav@557
|
125 |
extends AbstractMap<K,V>
|
jaroslav@557
|
126 |
implements Map<K,V>, Cloneable, Serializable
|
jaroslav@557
|
127 |
{
|
jaroslav@557
|
128 |
|
jaroslav@557
|
129 |
/**
|
jaroslav@557
|
130 |
* The default initial capacity - MUST be a power of two.
|
jaroslav@557
|
131 |
*/
|
jaroslav@557
|
132 |
static final int DEFAULT_INITIAL_CAPACITY = 16;
|
jaroslav@557
|
133 |
|
jaroslav@557
|
134 |
/**
|
jaroslav@557
|
135 |
* The maximum capacity, used if a higher value is implicitly specified
|
jaroslav@557
|
136 |
* by either of the constructors with arguments.
|
jaroslav@557
|
137 |
* MUST be a power of two <= 1<<30.
|
jaroslav@557
|
138 |
*/
|
jaroslav@557
|
139 |
static final int MAXIMUM_CAPACITY = 1 << 30;
|
jaroslav@557
|
140 |
|
jaroslav@557
|
141 |
/**
|
jaroslav@557
|
142 |
* The load factor used when none specified in constructor.
|
jaroslav@557
|
143 |
*/
|
jaroslav@557
|
144 |
static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
jaroslav@557
|
145 |
|
jaroslav@557
|
146 |
/**
|
jaroslav@557
|
147 |
* The table, resized as necessary. Length MUST Always be a power of two.
|
jaroslav@557
|
148 |
*/
|
jaroslav@557
|
149 |
transient Entry[] table;
|
jaroslav@557
|
150 |
|
jaroslav@557
|
151 |
/**
|
jaroslav@557
|
152 |
* The number of key-value mappings contained in this map.
|
jaroslav@557
|
153 |
*/
|
jaroslav@557
|
154 |
transient int size;
|
jaroslav@557
|
155 |
|
jaroslav@557
|
156 |
/**
|
jaroslav@557
|
157 |
* The next size value at which to resize (capacity * load factor).
|
jaroslav@557
|
158 |
* @serial
|
jaroslav@557
|
159 |
*/
|
jaroslav@557
|
160 |
int threshold;
|
jaroslav@557
|
161 |
|
jaroslav@557
|
162 |
/**
|
jaroslav@557
|
163 |
* The load factor for the hash table.
|
jaroslav@557
|
164 |
*
|
jaroslav@557
|
165 |
* @serial
|
jaroslav@557
|
166 |
*/
|
jaroslav@557
|
167 |
final float loadFactor;
|
jaroslav@557
|
168 |
|
jaroslav@557
|
169 |
/**
|
jaroslav@557
|
170 |
* The number of times this HashMap has been structurally modified
|
jaroslav@557
|
171 |
* Structural modifications are those that change the number of mappings in
|
jaroslav@557
|
172 |
* the HashMap or otherwise modify its internal structure (e.g.,
|
jaroslav@557
|
173 |
* rehash). This field is used to make iterators on Collection-views of
|
jaroslav@557
|
174 |
* the HashMap fail-fast. (See ConcurrentModificationException).
|
jaroslav@557
|
175 |
*/
|
jaroslav@557
|
176 |
transient int modCount;
|
jaroslav@557
|
177 |
|
jaroslav@557
|
178 |
/**
|
jaroslav@557
|
179 |
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
jaroslav@557
|
180 |
* capacity and load factor.
|
jaroslav@557
|
181 |
*
|
jaroslav@557
|
182 |
* @param initialCapacity the initial capacity
|
jaroslav@557
|
183 |
* @param loadFactor the load factor
|
jaroslav@557
|
184 |
* @throws IllegalArgumentException if the initial capacity is negative
|
jaroslav@557
|
185 |
* or the load factor is nonpositive
|
jaroslav@557
|
186 |
*/
|
jaroslav@557
|
187 |
public HashMap(int initialCapacity, float loadFactor) {
|
jaroslav@557
|
188 |
if (initialCapacity < 0)
|
jaroslav@557
|
189 |
throw new IllegalArgumentException("Illegal initial capacity: " +
|
jaroslav@557
|
190 |
initialCapacity);
|
jaroslav@557
|
191 |
if (initialCapacity > MAXIMUM_CAPACITY)
|
jaroslav@557
|
192 |
initialCapacity = MAXIMUM_CAPACITY;
|
jaroslav@557
|
193 |
if (loadFactor <= 0 || Float.isNaN(loadFactor))
|
jaroslav@557
|
194 |
throw new IllegalArgumentException("Illegal load factor: " +
|
jaroslav@557
|
195 |
loadFactor);
|
jaroslav@557
|
196 |
|
jaroslav@557
|
197 |
// Find a power of 2 >= initialCapacity
|
jaroslav@557
|
198 |
int capacity = 1;
|
jaroslav@557
|
199 |
while (capacity < initialCapacity)
|
jaroslav@557
|
200 |
capacity <<= 1;
|
jaroslav@557
|
201 |
|
jaroslav@557
|
202 |
this.loadFactor = loadFactor;
|
jaroslav@557
|
203 |
threshold = (int)(capacity * loadFactor);
|
jaroslav@557
|
204 |
table = new Entry[capacity];
|
jaroslav@557
|
205 |
init();
|
jaroslav@557
|
206 |
}
|
jaroslav@557
|
207 |
|
jaroslav@557
|
208 |
/**
|
jaroslav@557
|
209 |
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
jaroslav@557
|
210 |
* capacity and the default load factor (0.75).
|
jaroslav@557
|
211 |
*
|
jaroslav@557
|
212 |
* @param initialCapacity the initial capacity.
|
jaroslav@557
|
213 |
* @throws IllegalArgumentException if the initial capacity is negative.
|
jaroslav@557
|
214 |
*/
|
jaroslav@557
|
215 |
public HashMap(int initialCapacity) {
|
jaroslav@557
|
216 |
this(initialCapacity, DEFAULT_LOAD_FACTOR);
|
jaroslav@557
|
217 |
}
|
jaroslav@557
|
218 |
|
jaroslav@557
|
219 |
/**
|
jaroslav@557
|
220 |
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
|
jaroslav@557
|
221 |
* (16) and the default load factor (0.75).
|
jaroslav@557
|
222 |
*/
|
jaroslav@557
|
223 |
public HashMap() {
|
jaroslav@557
|
224 |
this.loadFactor = DEFAULT_LOAD_FACTOR;
|
jaroslav@557
|
225 |
threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
|
jaroslav@557
|
226 |
table = new Entry[DEFAULT_INITIAL_CAPACITY];
|
jaroslav@557
|
227 |
init();
|
jaroslav@557
|
228 |
}
|
jaroslav@557
|
229 |
|
jaroslav@557
|
230 |
/**
|
jaroslav@557
|
231 |
* Constructs a new <tt>HashMap</tt> with the same mappings as the
|
jaroslav@557
|
232 |
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
|
jaroslav@557
|
233 |
* default load factor (0.75) and an initial capacity sufficient to
|
jaroslav@557
|
234 |
* hold the mappings in the specified <tt>Map</tt>.
|
jaroslav@557
|
235 |
*
|
jaroslav@557
|
236 |
* @param m the map whose mappings are to be placed in this map
|
jaroslav@557
|
237 |
* @throws NullPointerException if the specified map is null
|
jaroslav@557
|
238 |
*/
|
jaroslav@557
|
239 |
public HashMap(Map<? extends K, ? extends V> m) {
|
jaroslav@557
|
240 |
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
|
jaroslav@557
|
241 |
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
|
jaroslav@557
|
242 |
putAllForCreate(m);
|
jaroslav@557
|
243 |
}
|
jaroslav@557
|
244 |
|
jaroslav@557
|
245 |
// internal utilities
|
jaroslav@557
|
246 |
|
jaroslav@557
|
247 |
/**
|
jaroslav@557
|
248 |
* Initialization hook for subclasses. This method is called
|
jaroslav@557
|
249 |
* in all constructors and pseudo-constructors (clone, readObject)
|
jaroslav@557
|
250 |
* after HashMap has been initialized but before any entries have
|
jaroslav@557
|
251 |
* been inserted. (In the absence of this method, readObject would
|
jaroslav@557
|
252 |
* require explicit knowledge of subclasses.)
|
jaroslav@557
|
253 |
*/
|
jaroslav@557
|
254 |
void init() {
|
jaroslav@557
|
255 |
}
|
jaroslav@557
|
256 |
|
jaroslav@557
|
257 |
/**
|
jaroslav@557
|
258 |
* Applies a supplemental hash function to a given hashCode, which
|
jaroslav@557
|
259 |
* defends against poor quality hash functions. This is critical
|
jaroslav@557
|
260 |
* because HashMap uses power-of-two length hash tables, that
|
jaroslav@557
|
261 |
* otherwise encounter collisions for hashCodes that do not differ
|
jaroslav@557
|
262 |
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
|
jaroslav@557
|
263 |
*/
|
jaroslav@557
|
264 |
static int hash(int h) {
|
jaroslav@557
|
265 |
// This function ensures that hashCodes that differ only by
|
jaroslav@557
|
266 |
// constant multiples at each bit position have a bounded
|
jaroslav@557
|
267 |
// number of collisions (approximately 8 at default load factor).
|
jaroslav@557
|
268 |
h ^= (h >>> 20) ^ (h >>> 12);
|
jaroslav@557
|
269 |
return h ^ (h >>> 7) ^ (h >>> 4);
|
jaroslav@557
|
270 |
}
|
jaroslav@557
|
271 |
|
jaroslav@557
|
272 |
/**
|
jaroslav@557
|
273 |
* Returns index for hash code h.
|
jaroslav@557
|
274 |
*/
|
jaroslav@557
|
275 |
static int indexFor(int h, int length) {
|
jaroslav@557
|
276 |
return h & (length-1);
|
jaroslav@557
|
277 |
}
|
jaroslav@557
|
278 |
|
jaroslav@557
|
279 |
/**
|
jaroslav@557
|
280 |
* Returns the number of key-value mappings in this map.
|
jaroslav@557
|
281 |
*
|
jaroslav@557
|
282 |
* @return the number of key-value mappings in this map
|
jaroslav@557
|
283 |
*/
|
jaroslav@557
|
284 |
public int size() {
|
jaroslav@557
|
285 |
return size;
|
jaroslav@557
|
286 |
}
|
jaroslav@557
|
287 |
|
jaroslav@557
|
288 |
/**
|
jaroslav@557
|
289 |
* Returns <tt>true</tt> if this map contains no key-value mappings.
|
jaroslav@557
|
290 |
*
|
jaroslav@557
|
291 |
* @return <tt>true</tt> if this map contains no key-value mappings
|
jaroslav@557
|
292 |
*/
|
jaroslav@557
|
293 |
public boolean isEmpty() {
|
jaroslav@557
|
294 |
return size == 0;
|
jaroslav@557
|
295 |
}
|
jaroslav@557
|
296 |
|
jaroslav@557
|
297 |
/**
|
jaroslav@557
|
298 |
* Returns the value to which the specified key is mapped,
|
jaroslav@557
|
299 |
* or {@code null} if this map contains no mapping for the key.
|
jaroslav@557
|
300 |
*
|
jaroslav@557
|
301 |
* <p>More formally, if this map contains a mapping from a key
|
jaroslav@557
|
302 |
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
|
jaroslav@557
|
303 |
* key.equals(k))}, then this method returns {@code v}; otherwise
|
jaroslav@557
|
304 |
* it returns {@code null}. (There can be at most one such mapping.)
|
jaroslav@557
|
305 |
*
|
jaroslav@557
|
306 |
* <p>A return value of {@code null} does not <i>necessarily</i>
|
jaroslav@557
|
307 |
* indicate that the map contains no mapping for the key; it's also
|
jaroslav@557
|
308 |
* possible that the map explicitly maps the key to {@code null}.
|
jaroslav@557
|
309 |
* The {@link #containsKey containsKey} operation may be used to
|
jaroslav@557
|
310 |
* distinguish these two cases.
|
jaroslav@557
|
311 |
*
|
jaroslav@557
|
312 |
* @see #put(Object, Object)
|
jaroslav@557
|
313 |
*/
|
jaroslav@557
|
314 |
public V get(Object key) {
|
jaroslav@557
|
315 |
if (key == null)
|
jaroslav@557
|
316 |
return getForNullKey();
|
jaroslav@557
|
317 |
int hash = hash(key.hashCode());
|
jaroslav@557
|
318 |
for (Entry<K,V> e = table[indexFor(hash, table.length)];
|
jaroslav@557
|
319 |
e != null;
|
jaroslav@557
|
320 |
e = e.next) {
|
jaroslav@557
|
321 |
Object k;
|
jaroslav@557
|
322 |
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
|
jaroslav@557
|
323 |
return e.value;
|
jaroslav@557
|
324 |
}
|
jaroslav@557
|
325 |
return null;
|
jaroslav@557
|
326 |
}
|
jaroslav@557
|
327 |
|
jaroslav@557
|
328 |
/**
|
jaroslav@557
|
329 |
* Offloaded version of get() to look up null keys. Null keys map
|
jaroslav@557
|
330 |
* to index 0. This null case is split out into separate methods
|
jaroslav@557
|
331 |
* for the sake of performance in the two most commonly used
|
jaroslav@557
|
332 |
* operations (get and put), but incorporated with conditionals in
|
jaroslav@557
|
333 |
* others.
|
jaroslav@557
|
334 |
*/
|
jaroslav@557
|
335 |
private V getForNullKey() {
|
jaroslav@557
|
336 |
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
|
jaroslav@557
|
337 |
if (e.key == null)
|
jaroslav@557
|
338 |
return e.value;
|
jaroslav@557
|
339 |
}
|
jaroslav@557
|
340 |
return null;
|
jaroslav@557
|
341 |
}
|
jaroslav@557
|
342 |
|
jaroslav@557
|
343 |
/**
|
jaroslav@557
|
344 |
* Returns <tt>true</tt> if this map contains a mapping for the
|
jaroslav@557
|
345 |
* specified key.
|
jaroslav@557
|
346 |
*
|
jaroslav@557
|
347 |
* @param key The key whose presence in this map is to be tested
|
jaroslav@557
|
348 |
* @return <tt>true</tt> if this map contains a mapping for the specified
|
jaroslav@557
|
349 |
* key.
|
jaroslav@557
|
350 |
*/
|
jaroslav@557
|
351 |
public boolean containsKey(Object key) {
|
jaroslav@557
|
352 |
return getEntry(key) != null;
|
jaroslav@557
|
353 |
}
|
jaroslav@557
|
354 |
|
jaroslav@557
|
355 |
/**
|
jaroslav@557
|
356 |
* Returns the entry associated with the specified key in the
|
jaroslav@557
|
357 |
* HashMap. Returns null if the HashMap contains no mapping
|
jaroslav@557
|
358 |
* for the key.
|
jaroslav@557
|
359 |
*/
|
jaroslav@557
|
360 |
final Entry<K,V> getEntry(Object key) {
|
jaroslav@557
|
361 |
int hash = (key == null) ? 0 : hash(key.hashCode());
|
jaroslav@557
|
362 |
for (Entry<K,V> e = table[indexFor(hash, table.length)];
|
jaroslav@557
|
363 |
e != null;
|
jaroslav@557
|
364 |
e = e.next) {
|
jaroslav@557
|
365 |
Object k;
|
jaroslav@557
|
366 |
if (e.hash == hash &&
|
jaroslav@557
|
367 |
((k = e.key) == key || (key != null && key.equals(k))))
|
jaroslav@557
|
368 |
return e;
|
jaroslav@557
|
369 |
}
|
jaroslav@557
|
370 |
return null;
|
jaroslav@557
|
371 |
}
|
jaroslav@557
|
372 |
|
jaroslav@557
|
373 |
|
jaroslav@557
|
374 |
/**
|
jaroslav@557
|
375 |
* Associates the specified value with the specified key in this map.
|
jaroslav@557
|
376 |
* If the map previously contained a mapping for the key, the old
|
jaroslav@557
|
377 |
* value is replaced.
|
jaroslav@557
|
378 |
*
|
jaroslav@557
|
379 |
* @param key key with which the specified value is to be associated
|
jaroslav@557
|
380 |
* @param value value to be associated with the specified key
|
jaroslav@557
|
381 |
* @return the previous value associated with <tt>key</tt>, or
|
jaroslav@557
|
382 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
|
jaroslav@557
|
383 |
* (A <tt>null</tt> return can also indicate that the map
|
jaroslav@557
|
384 |
* previously associated <tt>null</tt> with <tt>key</tt>.)
|
jaroslav@557
|
385 |
*/
|
jaroslav@557
|
386 |
public V put(K key, V value) {
|
jaroslav@557
|
387 |
if (key == null)
|
jaroslav@557
|
388 |
return putForNullKey(value);
|
jaroslav@557
|
389 |
int hash = hash(key.hashCode());
|
jaroslav@557
|
390 |
int i = indexFor(hash, table.length);
|
jaroslav@557
|
391 |
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
|
jaroslav@557
|
392 |
Object k;
|
jaroslav@557
|
393 |
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
|
jaroslav@557
|
394 |
V oldValue = e.value;
|
jaroslav@557
|
395 |
e.value = value;
|
jaroslav@557
|
396 |
e.recordAccess(this);
|
jaroslav@557
|
397 |
return oldValue;
|
jaroslav@557
|
398 |
}
|
jaroslav@557
|
399 |
}
|
jaroslav@557
|
400 |
|
jaroslav@557
|
401 |
modCount++;
|
jaroslav@557
|
402 |
addEntry(hash, key, value, i);
|
jaroslav@557
|
403 |
return null;
|
jaroslav@557
|
404 |
}
|
jaroslav@557
|
405 |
|
jaroslav@557
|
406 |
/**
|
jaroslav@557
|
407 |
* Offloaded version of put for null keys
|
jaroslav@557
|
408 |
*/
|
jaroslav@557
|
409 |
private V putForNullKey(V value) {
|
jaroslav@557
|
410 |
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
|
jaroslav@557
|
411 |
if (e.key == null) {
|
jaroslav@557
|
412 |
V oldValue = e.value;
|
jaroslav@557
|
413 |
e.value = value;
|
jaroslav@557
|
414 |
e.recordAccess(this);
|
jaroslav@557
|
415 |
return oldValue;
|
jaroslav@557
|
416 |
}
|
jaroslav@557
|
417 |
}
|
jaroslav@557
|
418 |
modCount++;
|
jaroslav@557
|
419 |
addEntry(0, null, value, 0);
|
jaroslav@557
|
420 |
return null;
|
jaroslav@557
|
421 |
}
|
jaroslav@557
|
422 |
|
jaroslav@557
|
423 |
/**
|
jaroslav@557
|
424 |
* This method is used instead of put by constructors and
|
jaroslav@557
|
425 |
* pseudoconstructors (clone, readObject). It does not resize the table,
|
jaroslav@557
|
426 |
* check for comodification, etc. It calls createEntry rather than
|
jaroslav@557
|
427 |
* addEntry.
|
jaroslav@557
|
428 |
*/
|
jaroslav@557
|
429 |
private void putForCreate(K key, V value) {
|
jaroslav@557
|
430 |
int hash = (key == null) ? 0 : hash(key.hashCode());
|
jaroslav@557
|
431 |
int i = indexFor(hash, table.length);
|
jaroslav@557
|
432 |
|
jaroslav@557
|
433 |
/**
|
jaroslav@557
|
434 |
* Look for preexisting entry for key. This will never happen for
|
jaroslav@557
|
435 |
* clone or deserialize. It will only happen for construction if the
|
jaroslav@557
|
436 |
* input Map is a sorted map whose ordering is inconsistent w/ equals.
|
jaroslav@557
|
437 |
*/
|
jaroslav@557
|
438 |
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
|
jaroslav@557
|
439 |
Object k;
|
jaroslav@557
|
440 |
if (e.hash == hash &&
|
jaroslav@557
|
441 |
((k = e.key) == key || (key != null && key.equals(k)))) {
|
jaroslav@557
|
442 |
e.value = value;
|
jaroslav@557
|
443 |
return;
|
jaroslav@557
|
444 |
}
|
jaroslav@557
|
445 |
}
|
jaroslav@557
|
446 |
|
jaroslav@557
|
447 |
createEntry(hash, key, value, i);
|
jaroslav@557
|
448 |
}
|
jaroslav@557
|
449 |
|
jaroslav@557
|
450 |
private void putAllForCreate(Map<? extends K, ? extends V> m) {
|
jaroslav@557
|
451 |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
|
jaroslav@557
|
452 |
putForCreate(e.getKey(), e.getValue());
|
jaroslav@557
|
453 |
}
|
jaroslav@557
|
454 |
|
jaroslav@557
|
455 |
/**
|
jaroslav@557
|
456 |
* Rehashes the contents of this map into a new array with a
|
jaroslav@557
|
457 |
* larger capacity. This method is called automatically when the
|
jaroslav@557
|
458 |
* number of keys in this map reaches its threshold.
|
jaroslav@557
|
459 |
*
|
jaroslav@557
|
460 |
* If current capacity is MAXIMUM_CAPACITY, this method does not
|
jaroslav@557
|
461 |
* resize the map, but sets threshold to Integer.MAX_VALUE.
|
jaroslav@557
|
462 |
* This has the effect of preventing future calls.
|
jaroslav@557
|
463 |
*
|
jaroslav@557
|
464 |
* @param newCapacity the new capacity, MUST be a power of two;
|
jaroslav@557
|
465 |
* must be greater than current capacity unless current
|
jaroslav@557
|
466 |
* capacity is MAXIMUM_CAPACITY (in which case value
|
jaroslav@557
|
467 |
* is irrelevant).
|
jaroslav@557
|
468 |
*/
|
jaroslav@557
|
469 |
void resize(int newCapacity) {
|
jaroslav@557
|
470 |
Entry[] oldTable = table;
|
jaroslav@557
|
471 |
int oldCapacity = oldTable.length;
|
jaroslav@557
|
472 |
if (oldCapacity == MAXIMUM_CAPACITY) {
|
jaroslav@557
|
473 |
threshold = Integer.MAX_VALUE;
|
jaroslav@557
|
474 |
return;
|
jaroslav@557
|
475 |
}
|
jaroslav@557
|
476 |
|
jaroslav@557
|
477 |
Entry[] newTable = new Entry[newCapacity];
|
jaroslav@557
|
478 |
transfer(newTable);
|
jaroslav@557
|
479 |
table = newTable;
|
jaroslav@557
|
480 |
threshold = (int)(newCapacity * loadFactor);
|
jaroslav@557
|
481 |
}
|
jaroslav@557
|
482 |
|
jaroslav@557
|
483 |
/**
|
jaroslav@557
|
484 |
* Transfers all entries from current table to newTable.
|
jaroslav@557
|
485 |
*/
|
jaroslav@557
|
486 |
void transfer(Entry[] newTable) {
|
jaroslav@557
|
487 |
Entry[] src = table;
|
jaroslav@557
|
488 |
int newCapacity = newTable.length;
|
jaroslav@557
|
489 |
for (int j = 0; j < src.length; j++) {
|
jaroslav@557
|
490 |
Entry<K,V> e = src[j];
|
jaroslav@557
|
491 |
if (e != null) {
|
jaroslav@557
|
492 |
src[j] = null;
|
jaroslav@557
|
493 |
do {
|
jaroslav@557
|
494 |
Entry<K,V> next = e.next;
|
jaroslav@557
|
495 |
int i = indexFor(e.hash, newCapacity);
|
jaroslav@557
|
496 |
e.next = newTable[i];
|
jaroslav@557
|
497 |
newTable[i] = e;
|
jaroslav@557
|
498 |
e = next;
|
jaroslav@557
|
499 |
} while (e != null);
|
jaroslav@557
|
500 |
}
|
jaroslav@557
|
501 |
}
|
jaroslav@557
|
502 |
}
|
jaroslav@557
|
503 |
|
jaroslav@557
|
504 |
/**
|
jaroslav@557
|
505 |
* Copies all of the mappings from the specified map to this map.
|
jaroslav@557
|
506 |
* These mappings will replace any mappings that this map had for
|
jaroslav@557
|
507 |
* any of the keys currently in the specified map.
|
jaroslav@557
|
508 |
*
|
jaroslav@557
|
509 |
* @param m mappings to be stored in this map
|
jaroslav@557
|
510 |
* @throws NullPointerException if the specified map is null
|
jaroslav@557
|
511 |
*/
|
jaroslav@557
|
512 |
public void putAll(Map<? extends K, ? extends V> m) {
|
jaroslav@557
|
513 |
int numKeysToBeAdded = m.size();
|
jaroslav@557
|
514 |
if (numKeysToBeAdded == 0)
|
jaroslav@557
|
515 |
return;
|
jaroslav@557
|
516 |
|
jaroslav@557
|
517 |
/*
|
jaroslav@557
|
518 |
* Expand the map if the map if the number of mappings to be added
|
jaroslav@557
|
519 |
* is greater than or equal to threshold. This is conservative; the
|
jaroslav@557
|
520 |
* obvious condition is (m.size() + size) >= threshold, but this
|
jaroslav@557
|
521 |
* condition could result in a map with twice the appropriate capacity,
|
jaroslav@557
|
522 |
* if the keys to be added overlap with the keys already in this map.
|
jaroslav@557
|
523 |
* By using the conservative calculation, we subject ourself
|
jaroslav@557
|
524 |
* to at most one extra resize.
|
jaroslav@557
|
525 |
*/
|
jaroslav@557
|
526 |
if (numKeysToBeAdded > threshold) {
|
jaroslav@557
|
527 |
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
|
jaroslav@557
|
528 |
if (targetCapacity > MAXIMUM_CAPACITY)
|
jaroslav@557
|
529 |
targetCapacity = MAXIMUM_CAPACITY;
|
jaroslav@557
|
530 |
int newCapacity = table.length;
|
jaroslav@557
|
531 |
while (newCapacity < targetCapacity)
|
jaroslav@557
|
532 |
newCapacity <<= 1;
|
jaroslav@557
|
533 |
if (newCapacity > table.length)
|
jaroslav@557
|
534 |
resize(newCapacity);
|
jaroslav@557
|
535 |
}
|
jaroslav@557
|
536 |
|
jaroslav@557
|
537 |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
|
jaroslav@557
|
538 |
put(e.getKey(), e.getValue());
|
jaroslav@557
|
539 |
}
|
jaroslav@557
|
540 |
|
jaroslav@557
|
541 |
/**
|
jaroslav@557
|
542 |
* Removes the mapping for the specified key from this map if present.
|
jaroslav@557
|
543 |
*
|
jaroslav@557
|
544 |
* @param key key whose mapping is to be removed from the map
|
jaroslav@557
|
545 |
* @return the previous value associated with <tt>key</tt>, or
|
jaroslav@557
|
546 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
|
jaroslav@557
|
547 |
* (A <tt>null</tt> return can also indicate that the map
|
jaroslav@557
|
548 |
* previously associated <tt>null</tt> with <tt>key</tt>.)
|
jaroslav@557
|
549 |
*/
|
jaroslav@557
|
550 |
public V remove(Object key) {
|
jaroslav@557
|
551 |
Entry<K,V> e = removeEntryForKey(key);
|
jaroslav@557
|
552 |
return (e == null ? null : e.value);
|
jaroslav@557
|
553 |
}
|
jaroslav@557
|
554 |
|
jaroslav@557
|
555 |
/**
|
jaroslav@557
|
556 |
* Removes and returns the entry associated with the specified key
|
jaroslav@557
|
557 |
* in the HashMap. Returns null if the HashMap contains no mapping
|
jaroslav@557
|
558 |
* for this key.
|
jaroslav@557
|
559 |
*/
|
jaroslav@557
|
560 |
final Entry<K,V> removeEntryForKey(Object key) {
|
jaroslav@557
|
561 |
int hash = (key == null) ? 0 : hash(key.hashCode());
|
jaroslav@557
|
562 |
int i = indexFor(hash, table.length);
|
jaroslav@557
|
563 |
Entry<K,V> prev = table[i];
|
jaroslav@557
|
564 |
Entry<K,V> e = prev;
|
jaroslav@557
|
565 |
|
jaroslav@557
|
566 |
while (e != null) {
|
jaroslav@557
|
567 |
Entry<K,V> next = e.next;
|
jaroslav@557
|
568 |
Object k;
|
jaroslav@557
|
569 |
if (e.hash == hash &&
|
jaroslav@557
|
570 |
((k = e.key) == key || (key != null && key.equals(k)))) {
|
jaroslav@557
|
571 |
modCount++;
|
jaroslav@557
|
572 |
size--;
|
jaroslav@557
|
573 |
if (prev == e)
|
jaroslav@557
|
574 |
table[i] = next;
|
jaroslav@557
|
575 |
else
|
jaroslav@557
|
576 |
prev.next = next;
|
jaroslav@557
|
577 |
e.recordRemoval(this);
|
jaroslav@557
|
578 |
return e;
|
jaroslav@557
|
579 |
}
|
jaroslav@557
|
580 |
prev = e;
|
jaroslav@557
|
581 |
e = next;
|
jaroslav@557
|
582 |
}
|
jaroslav@557
|
583 |
|
jaroslav@557
|
584 |
return e;
|
jaroslav@557
|
585 |
}
|
jaroslav@557
|
586 |
|
jaroslav@557
|
587 |
/**
|
jaroslav@557
|
588 |
* Special version of remove for EntrySet.
|
jaroslav@557
|
589 |
*/
|
jaroslav@557
|
590 |
final Entry<K,V> removeMapping(Object o) {
|
jaroslav@557
|
591 |
if (!(o instanceof Map.Entry))
|
jaroslav@557
|
592 |
return null;
|
jaroslav@557
|
593 |
|
jaroslav@557
|
594 |
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
|
jaroslav@557
|
595 |
Object key = entry.getKey();
|
jaroslav@557
|
596 |
int hash = (key == null) ? 0 : hash(key.hashCode());
|
jaroslav@557
|
597 |
int i = indexFor(hash, table.length);
|
jaroslav@557
|
598 |
Entry<K,V> prev = table[i];
|
jaroslav@557
|
599 |
Entry<K,V> e = prev;
|
jaroslav@557
|
600 |
|
jaroslav@557
|
601 |
while (e != null) {
|
jaroslav@557
|
602 |
Entry<K,V> next = e.next;
|
jaroslav@557
|
603 |
if (e.hash == hash && e.equals(entry)) {
|
jaroslav@557
|
604 |
modCount++;
|
jaroslav@557
|
605 |
size--;
|
jaroslav@557
|
606 |
if (prev == e)
|
jaroslav@557
|
607 |
table[i] = next;
|
jaroslav@557
|
608 |
else
|
jaroslav@557
|
609 |
prev.next = next;
|
jaroslav@557
|
610 |
e.recordRemoval(this);
|
jaroslav@557
|
611 |
return e;
|
jaroslav@557
|
612 |
}
|
jaroslav@557
|
613 |
prev = e;
|
jaroslav@557
|
614 |
e = next;
|
jaroslav@557
|
615 |
}
|
jaroslav@557
|
616 |
|
jaroslav@557
|
617 |
return e;
|
jaroslav@557
|
618 |
}
|
jaroslav@557
|
619 |
|
jaroslav@557
|
620 |
/**
|
jaroslav@557
|
621 |
* Removes all of the mappings from this map.
|
jaroslav@557
|
622 |
* The map will be empty after this call returns.
|
jaroslav@557
|
623 |
*/
|
jaroslav@557
|
624 |
public void clear() {
|
jaroslav@557
|
625 |
modCount++;
|
jaroslav@557
|
626 |
Entry[] tab = table;
|
jaroslav@557
|
627 |
for (int i = 0; i < tab.length; i++)
|
jaroslav@557
|
628 |
tab[i] = null;
|
jaroslav@557
|
629 |
size = 0;
|
jaroslav@557
|
630 |
}
|
jaroslav@557
|
631 |
|
jaroslav@557
|
632 |
/**
|
jaroslav@557
|
633 |
* Returns <tt>true</tt> if this map maps one or more keys to the
|
jaroslav@557
|
634 |
* specified value.
|
jaroslav@557
|
635 |
*
|
jaroslav@557
|
636 |
* @param value value whose presence in this map is to be tested
|
jaroslav@557
|
637 |
* @return <tt>true</tt> if this map maps one or more keys to the
|
jaroslav@557
|
638 |
* specified value
|
jaroslav@557
|
639 |
*/
|
jaroslav@557
|
640 |
public boolean containsValue(Object value) {
|
jaroslav@557
|
641 |
if (value == null)
|
jaroslav@557
|
642 |
return containsNullValue();
|
jaroslav@557
|
643 |
|
jaroslav@557
|
644 |
Entry[] tab = table;
|
jaroslav@557
|
645 |
for (int i = 0; i < tab.length ; i++)
|
jaroslav@557
|
646 |
for (Entry e = tab[i] ; e != null ; e = e.next)
|
jaroslav@557
|
647 |
if (value.equals(e.value))
|
jaroslav@557
|
648 |
return true;
|
jaroslav@557
|
649 |
return false;
|
jaroslav@557
|
650 |
}
|
jaroslav@557
|
651 |
|
jaroslav@557
|
652 |
/**
|
jaroslav@557
|
653 |
* Special-case code for containsValue with null argument
|
jaroslav@557
|
654 |
*/
|
jaroslav@557
|
655 |
private boolean containsNullValue() {
|
jaroslav@557
|
656 |
Entry[] tab = table;
|
jaroslav@557
|
657 |
for (int i = 0; i < tab.length ; i++)
|
jaroslav@557
|
658 |
for (Entry e = tab[i] ; e != null ; e = e.next)
|
jaroslav@557
|
659 |
if (e.value == null)
|
jaroslav@557
|
660 |
return true;
|
jaroslav@557
|
661 |
return false;
|
jaroslav@557
|
662 |
}
|
jaroslav@557
|
663 |
|
jaroslav@557
|
664 |
/**
|
jaroslav@557
|
665 |
* Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
|
jaroslav@557
|
666 |
* values themselves are not cloned.
|
jaroslav@557
|
667 |
*
|
jaroslav@557
|
668 |
* @return a shallow copy of this map
|
jaroslav@557
|
669 |
*/
|
jaroslav@557
|
670 |
public Object clone() {
|
jaroslav@557
|
671 |
HashMap<K,V> result = null;
|
jaroslav@557
|
672 |
try {
|
jaroslav@557
|
673 |
result = (HashMap<K,V>)super.clone();
|
jaroslav@557
|
674 |
} catch (CloneNotSupportedException e) {
|
jaroslav@557
|
675 |
// assert false;
|
jaroslav@557
|
676 |
}
|
jaroslav@557
|
677 |
result.table = new Entry[table.length];
|
jaroslav@557
|
678 |
result.entrySet = null;
|
jaroslav@557
|
679 |
result.modCount = 0;
|
jaroslav@557
|
680 |
result.size = 0;
|
jaroslav@557
|
681 |
result.init();
|
jaroslav@557
|
682 |
result.putAllForCreate(this);
|
jaroslav@557
|
683 |
|
jaroslav@557
|
684 |
return result;
|
jaroslav@557
|
685 |
}
|
jaroslav@557
|
686 |
|
jaroslav@557
|
687 |
static class Entry<K,V> implements Map.Entry<K,V> {
|
jaroslav@557
|
688 |
final K key;
|
jaroslav@557
|
689 |
V value;
|
jaroslav@557
|
690 |
Entry<K,V> next;
|
jaroslav@557
|
691 |
final int hash;
|
jaroslav@557
|
692 |
|
jaroslav@557
|
693 |
/**
|
jaroslav@557
|
694 |
* Creates new entry.
|
jaroslav@557
|
695 |
*/
|
jaroslav@557
|
696 |
Entry(int h, K k, V v, Entry<K,V> n) {
|
jaroslav@557
|
697 |
value = v;
|
jaroslav@557
|
698 |
next = n;
|
jaroslav@557
|
699 |
key = k;
|
jaroslav@557
|
700 |
hash = h;
|
jaroslav@557
|
701 |
}
|
jaroslav@557
|
702 |
|
jaroslav@557
|
703 |
public final K getKey() {
|
jaroslav@557
|
704 |
return key;
|
jaroslav@557
|
705 |
}
|
jaroslav@557
|
706 |
|
jaroslav@557
|
707 |
public final V getValue() {
|
jaroslav@557
|
708 |
return value;
|
jaroslav@557
|
709 |
}
|
jaroslav@557
|
710 |
|
jaroslav@557
|
711 |
public final V setValue(V newValue) {
|
jaroslav@557
|
712 |
V oldValue = value;
|
jaroslav@557
|
713 |
value = newValue;
|
jaroslav@557
|
714 |
return oldValue;
|
jaroslav@557
|
715 |
}
|
jaroslav@557
|
716 |
|
jaroslav@557
|
717 |
public final boolean equals(Object o) {
|
jaroslav@557
|
718 |
if (!(o instanceof Map.Entry))
|
jaroslav@557
|
719 |
return false;
|
jaroslav@557
|
720 |
Map.Entry e = (Map.Entry)o;
|
jaroslav@557
|
721 |
Object k1 = getKey();
|
jaroslav@557
|
722 |
Object k2 = e.getKey();
|
jaroslav@557
|
723 |
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
|
jaroslav@557
|
724 |
Object v1 = getValue();
|
jaroslav@557
|
725 |
Object v2 = e.getValue();
|
jaroslav@557
|
726 |
if (v1 == v2 || (v1 != null && v1.equals(v2)))
|
jaroslav@557
|
727 |
return true;
|
jaroslav@557
|
728 |
}
|
jaroslav@557
|
729 |
return false;
|
jaroslav@557
|
730 |
}
|
jaroslav@557
|
731 |
|
jaroslav@557
|
732 |
public final int hashCode() {
|
jaroslav@557
|
733 |
return (key==null ? 0 : key.hashCode()) ^
|
jaroslav@557
|
734 |
(value==null ? 0 : value.hashCode());
|
jaroslav@557
|
735 |
}
|
jaroslav@557
|
736 |
|
jaroslav@557
|
737 |
public final String toString() {
|
jaroslav@557
|
738 |
return getKey() + "=" + getValue();
|
jaroslav@557
|
739 |
}
|
jaroslav@557
|
740 |
|
jaroslav@557
|
741 |
/**
|
jaroslav@557
|
742 |
* This method is invoked whenever the value in an entry is
|
jaroslav@557
|
743 |
* overwritten by an invocation of put(k,v) for a key k that's already
|
jaroslav@557
|
744 |
* in the HashMap.
|
jaroslav@557
|
745 |
*/
|
jaroslav@557
|
746 |
void recordAccess(HashMap<K,V> m) {
|
jaroslav@557
|
747 |
}
|
jaroslav@557
|
748 |
|
jaroslav@557
|
749 |
/**
|
jaroslav@557
|
750 |
* This method is invoked whenever the entry is
|
jaroslav@557
|
751 |
* removed from the table.
|
jaroslav@557
|
752 |
*/
|
jaroslav@557
|
753 |
void recordRemoval(HashMap<K,V> m) {
|
jaroslav@557
|
754 |
}
|
jaroslav@557
|
755 |
}
|
jaroslav@557
|
756 |
|
jaroslav@557
|
757 |
/**
|
jaroslav@557
|
758 |
* Adds a new entry with the specified key, value and hash code to
|
jaroslav@557
|
759 |
* the specified bucket. It is the responsibility of this
|
jaroslav@557
|
760 |
* method to resize the table if appropriate.
|
jaroslav@557
|
761 |
*
|
jaroslav@557
|
762 |
* Subclass overrides this to alter the behavior of put method.
|
jaroslav@557
|
763 |
*/
|
jaroslav@557
|
764 |
void addEntry(int hash, K key, V value, int bucketIndex) {
|
jaroslav@557
|
765 |
Entry<K,V> e = table[bucketIndex];
|
jaroslav@557
|
766 |
table[bucketIndex] = new Entry<>(hash, key, value, e);
|
jaroslav@557
|
767 |
if (size++ >= threshold)
|
jaroslav@557
|
768 |
resize(2 * table.length);
|
jaroslav@557
|
769 |
}
|
jaroslav@557
|
770 |
|
jaroslav@557
|
771 |
/**
|
jaroslav@557
|
772 |
* Like addEntry except that this version is used when creating entries
|
jaroslav@557
|
773 |
* as part of Map construction or "pseudo-construction" (cloning,
|
jaroslav@557
|
774 |
* deserialization). This version needn't worry about resizing the table.
|
jaroslav@557
|
775 |
*
|
jaroslav@557
|
776 |
* Subclass overrides this to alter the behavior of HashMap(Map),
|
jaroslav@557
|
777 |
* clone, and readObject.
|
jaroslav@557
|
778 |
*/
|
jaroslav@557
|
779 |
void createEntry(int hash, K key, V value, int bucketIndex) {
|
jaroslav@557
|
780 |
Entry<K,V> e = table[bucketIndex];
|
jaroslav@557
|
781 |
table[bucketIndex] = new Entry<>(hash, key, value, e);
|
jaroslav@557
|
782 |
size++;
|
jaroslav@557
|
783 |
}
|
jaroslav@557
|
784 |
|
jaroslav@557
|
785 |
private abstract class HashIterator<E> implements Iterator<E> {
|
jaroslav@557
|
786 |
Entry<K,V> next; // next entry to return
|
jaroslav@557
|
787 |
int expectedModCount; // For fast-fail
|
jaroslav@557
|
788 |
int index; // current slot
|
jaroslav@557
|
789 |
Entry<K,V> current; // current entry
|
jaroslav@557
|
790 |
|
jaroslav@557
|
791 |
HashIterator() {
|
jaroslav@557
|
792 |
expectedModCount = modCount;
|
jaroslav@557
|
793 |
if (size > 0) { // advance to first entry
|
jaroslav@557
|
794 |
Entry[] t = table;
|
jaroslav@557
|
795 |
while (index < t.length && (next = t[index++]) == null)
|
jaroslav@557
|
796 |
;
|
jaroslav@557
|
797 |
}
|
jaroslav@557
|
798 |
}
|
jaroslav@557
|
799 |
|
jaroslav@557
|
800 |
public final boolean hasNext() {
|
jaroslav@557
|
801 |
return next != null;
|
jaroslav@557
|
802 |
}
|
jaroslav@557
|
803 |
|
jaroslav@557
|
804 |
final Entry<K,V> nextEntry() {
|
jaroslav@557
|
805 |
if (modCount != expectedModCount)
|
jaroslav@557
|
806 |
throw new ConcurrentModificationException();
|
jaroslav@557
|
807 |
Entry<K,V> e = next;
|
jaroslav@557
|
808 |
if (e == null)
|
jaroslav@557
|
809 |
throw new NoSuchElementException();
|
jaroslav@557
|
810 |
|
jaroslav@557
|
811 |
if ((next = e.next) == null) {
|
jaroslav@557
|
812 |
Entry[] t = table;
|
jaroslav@557
|
813 |
while (index < t.length && (next = t[index++]) == null)
|
jaroslav@557
|
814 |
;
|
jaroslav@557
|
815 |
}
|
jaroslav@557
|
816 |
current = e;
|
jaroslav@557
|
817 |
return e;
|
jaroslav@557
|
818 |
}
|
jaroslav@557
|
819 |
|
jaroslav@557
|
820 |
public void remove() {
|
jaroslav@557
|
821 |
if (current == null)
|
jaroslav@557
|
822 |
throw new IllegalStateException();
|
jaroslav@557
|
823 |
if (modCount != expectedModCount)
|
jaroslav@557
|
824 |
throw new ConcurrentModificationException();
|
jaroslav@557
|
825 |
Object k = current.key;
|
jaroslav@557
|
826 |
current = null;
|
jaroslav@557
|
827 |
HashMap.this.removeEntryForKey(k);
|
jaroslav@557
|
828 |
expectedModCount = modCount;
|
jaroslav@557
|
829 |
}
|
jaroslav@557
|
830 |
|
jaroslav@557
|
831 |
}
|
jaroslav@557
|
832 |
|
jaroslav@557
|
833 |
private final class ValueIterator extends HashIterator<V> {
|
jaroslav@557
|
834 |
public V next() {
|
jaroslav@557
|
835 |
return nextEntry().value;
|
jaroslav@557
|
836 |
}
|
jaroslav@557
|
837 |
}
|
jaroslav@557
|
838 |
|
jaroslav@557
|
839 |
private final class KeyIterator extends HashIterator<K> {
|
jaroslav@557
|
840 |
public K next() {
|
jaroslav@557
|
841 |
return nextEntry().getKey();
|
jaroslav@557
|
842 |
}
|
jaroslav@557
|
843 |
}
|
jaroslav@557
|
844 |
|
jaroslav@557
|
845 |
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
|
jaroslav@557
|
846 |
public Map.Entry<K,V> next() {
|
jaroslav@557
|
847 |
return nextEntry();
|
jaroslav@557
|
848 |
}
|
jaroslav@557
|
849 |
}
|
jaroslav@557
|
850 |
|
jaroslav@557
|
851 |
// Subclass overrides these to alter behavior of views' iterator() method
|
jaroslav@557
|
852 |
Iterator<K> newKeyIterator() {
|
jaroslav@557
|
853 |
return new KeyIterator();
|
jaroslav@557
|
854 |
}
|
jaroslav@557
|
855 |
Iterator<V> newValueIterator() {
|
jaroslav@557
|
856 |
return new ValueIterator();
|
jaroslav@557
|
857 |
}
|
jaroslav@557
|
858 |
Iterator<Map.Entry<K,V>> newEntryIterator() {
|
jaroslav@557
|
859 |
return new EntryIterator();
|
jaroslav@557
|
860 |
}
|
jaroslav@557
|
861 |
|
jaroslav@557
|
862 |
|
jaroslav@557
|
863 |
// Views
|
jaroslav@557
|
864 |
|
jaroslav@557
|
865 |
private transient Set<Map.Entry<K,V>> entrySet = null;
|
jaroslav@557
|
866 |
|
jaroslav@557
|
867 |
/**
|
jaroslav@557
|
868 |
* Returns a {@link Set} view of the keys contained in this map.
|
jaroslav@557
|
869 |
* The set is backed by the map, so changes to the map are
|
jaroslav@557
|
870 |
* reflected in the set, and vice-versa. If the map is modified
|
jaroslav@557
|
871 |
* while an iteration over the set is in progress (except through
|
jaroslav@557
|
872 |
* the iterator's own <tt>remove</tt> operation), the results of
|
jaroslav@557
|
873 |
* the iteration are undefined. The set supports element removal,
|
jaroslav@557
|
874 |
* which removes the corresponding mapping from the map, via the
|
jaroslav@557
|
875 |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
|
jaroslav@557
|
876 |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
|
jaroslav@557
|
877 |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
|
jaroslav@557
|
878 |
* operations.
|
jaroslav@557
|
879 |
*/
|
jaroslav@557
|
880 |
public Set<K> keySet() {
|
jaroslav@557
|
881 |
Set<K> ks = keySet;
|
jaroslav@557
|
882 |
return (ks != null ? ks : (keySet = new KeySet()));
|
jaroslav@557
|
883 |
}
|
jaroslav@557
|
884 |
|
jaroslav@557
|
885 |
private final class KeySet extends AbstractSet<K> {
|
jaroslav@557
|
886 |
public Iterator<K> iterator() {
|
jaroslav@557
|
887 |
return newKeyIterator();
|
jaroslav@557
|
888 |
}
|
jaroslav@557
|
889 |
public int size() {
|
jaroslav@557
|
890 |
return size;
|
jaroslav@557
|
891 |
}
|
jaroslav@557
|
892 |
public boolean contains(Object o) {
|
jaroslav@557
|
893 |
return containsKey(o);
|
jaroslav@557
|
894 |
}
|
jaroslav@557
|
895 |
public boolean remove(Object o) {
|
jaroslav@557
|
896 |
return HashMap.this.removeEntryForKey(o) != null;
|
jaroslav@557
|
897 |
}
|
jaroslav@557
|
898 |
public void clear() {
|
jaroslav@557
|
899 |
HashMap.this.clear();
|
jaroslav@557
|
900 |
}
|
jaroslav@557
|
901 |
}
|
jaroslav@557
|
902 |
|
jaroslav@557
|
903 |
/**
|
jaroslav@557
|
904 |
* Returns a {@link Collection} view of the values contained in this map.
|
jaroslav@557
|
905 |
* The collection is backed by the map, so changes to the map are
|
jaroslav@557
|
906 |
* reflected in the collection, and vice-versa. If the map is
|
jaroslav@557
|
907 |
* modified while an iteration over the collection is in progress
|
jaroslav@557
|
908 |
* (except through the iterator's own <tt>remove</tt> operation),
|
jaroslav@557
|
909 |
* the results of the iteration are undefined. The collection
|
jaroslav@557
|
910 |
* supports element removal, which removes the corresponding
|
jaroslav@557
|
911 |
* mapping from the map, via the <tt>Iterator.remove</tt>,
|
jaroslav@557
|
912 |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>,
|
jaroslav@557
|
913 |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
|
jaroslav@557
|
914 |
* support the <tt>add</tt> or <tt>addAll</tt> operations.
|
jaroslav@557
|
915 |
*/
|
jaroslav@557
|
916 |
public Collection<V> values() {
|
jaroslav@557
|
917 |
Collection<V> vs = values;
|
jaroslav@557
|
918 |
return (vs != null ? vs : (values = new Values()));
|
jaroslav@557
|
919 |
}
|
jaroslav@557
|
920 |
|
jaroslav@557
|
921 |
private final class Values extends AbstractCollection<V> {
|
jaroslav@557
|
922 |
public Iterator<V> iterator() {
|
jaroslav@557
|
923 |
return newValueIterator();
|
jaroslav@557
|
924 |
}
|
jaroslav@557
|
925 |
public int size() {
|
jaroslav@557
|
926 |
return size;
|
jaroslav@557
|
927 |
}
|
jaroslav@557
|
928 |
public boolean contains(Object o) {
|
jaroslav@557
|
929 |
return containsValue(o);
|
jaroslav@557
|
930 |
}
|
jaroslav@557
|
931 |
public void clear() {
|
jaroslav@557
|
932 |
HashMap.this.clear();
|
jaroslav@557
|
933 |
}
|
jaroslav@557
|
934 |
}
|
jaroslav@557
|
935 |
|
jaroslav@557
|
936 |
/**
|
jaroslav@557
|
937 |
* Returns a {@link Set} view of the mappings contained in this map.
|
jaroslav@557
|
938 |
* The set is backed by the map, so changes to the map are
|
jaroslav@557
|
939 |
* reflected in the set, and vice-versa. If the map is modified
|
jaroslav@557
|
940 |
* while an iteration over the set is in progress (except through
|
jaroslav@557
|
941 |
* the iterator's own <tt>remove</tt> operation, or through the
|
jaroslav@557
|
942 |
* <tt>setValue</tt> operation on a map entry returned by the
|
jaroslav@557
|
943 |
* iterator) the results of the iteration are undefined. The set
|
jaroslav@557
|
944 |
* supports element removal, which removes the corresponding
|
jaroslav@557
|
945 |
* mapping from the map, via the <tt>Iterator.remove</tt>,
|
jaroslav@557
|
946 |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
|
jaroslav@557
|
947 |
* <tt>clear</tt> operations. It does not support the
|
jaroslav@557
|
948 |
* <tt>add</tt> or <tt>addAll</tt> operations.
|
jaroslav@557
|
949 |
*
|
jaroslav@557
|
950 |
* @return a set view of the mappings contained in this map
|
jaroslav@557
|
951 |
*/
|
jaroslav@557
|
952 |
public Set<Map.Entry<K,V>> entrySet() {
|
jaroslav@557
|
953 |
return entrySet0();
|
jaroslav@557
|
954 |
}
|
jaroslav@557
|
955 |
|
jaroslav@557
|
956 |
private Set<Map.Entry<K,V>> entrySet0() {
|
jaroslav@557
|
957 |
Set<Map.Entry<K,V>> es = entrySet;
|
jaroslav@557
|
958 |
return es != null ? es : (entrySet = new EntrySet());
|
jaroslav@557
|
959 |
}
|
jaroslav@557
|
960 |
|
jaroslav@557
|
961 |
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
|
jaroslav@557
|
962 |
public Iterator<Map.Entry<K,V>> iterator() {
|
jaroslav@557
|
963 |
return newEntryIterator();
|
jaroslav@557
|
964 |
}
|
jaroslav@557
|
965 |
public boolean contains(Object o) {
|
jaroslav@557
|
966 |
if (!(o instanceof Map.Entry))
|
jaroslav@557
|
967 |
return false;
|
jaroslav@557
|
968 |
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
|
jaroslav@557
|
969 |
Entry<K,V> candidate = getEntry(e.getKey());
|
jaroslav@557
|
970 |
return candidate != null && candidate.equals(e);
|
jaroslav@557
|
971 |
}
|
jaroslav@557
|
972 |
public boolean remove(Object o) {
|
jaroslav@557
|
973 |
return removeMapping(o) != null;
|
jaroslav@557
|
974 |
}
|
jaroslav@557
|
975 |
public int size() {
|
jaroslav@557
|
976 |
return size;
|
jaroslav@557
|
977 |
}
|
jaroslav@557
|
978 |
public void clear() {
|
jaroslav@557
|
979 |
HashMap.this.clear();
|
jaroslav@557
|
980 |
}
|
jaroslav@557
|
981 |
}
|
jaroslav@557
|
982 |
|
jaroslav@557
|
983 |
|
jaroslav@557
|
984 |
private static final long serialVersionUID = 362498820763181265L;
|
jaroslav@557
|
985 |
|
jaroslav@557
|
986 |
|
jaroslav@557
|
987 |
// These methods are used when serializing HashSets
|
jaroslav@557
|
988 |
int capacity() { return table.length; }
|
jaroslav@557
|
989 |
float loadFactor() { return loadFactor; }
|
jaroslav@557
|
990 |
}
|