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