author | naoto |
Thu, 14 Mar 2013 11:29:16 -0700 | |
changeset 16481 | 8e30386cc014 |
parent 14342 | 8435a30053c1 |
child 17168 | b7d3500f2516 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13018
diff
changeset
|
2 |
* Copyright (c) 1998, 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.lang.ref.WeakReference; |
|
28 |
import java.lang.ref.ReferenceQueue; |
|
29 |
||
30 |
||
31 |
/** |
|
56
48451b4616e8
5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents:
2
diff
changeset
|
32 |
* Hash table based implementation of the <tt>Map</tt> interface, with |
48451b4616e8
5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents:
2
diff
changeset
|
33 |
* <em>weak keys</em>. |
2 | 34 |
* An entry in a <tt>WeakHashMap</tt> will automatically be removed when |
35 |
* its key is no longer in ordinary use. More precisely, the presence of a |
|
36 |
* mapping for a given key will not prevent the key from being discarded by the |
|
37 |
* garbage collector, that is, made finalizable, finalized, and then reclaimed. |
|
38 |
* When a key has been discarded its entry is effectively removed from the map, |
|
39 |
* so this class behaves somewhat differently from other <tt>Map</tt> |
|
40 |
* implementations. |
|
41 |
* |
|
42 |
* <p> Both null values and the null key are supported. This class has |
|
43 |
* performance characteristics similar to those of the <tt>HashMap</tt> |
|
44 |
* class, and has the same efficiency parameters of <em>initial capacity</em> |
|
45 |
* and <em>load factor</em>. |
|
46 |
* |
|
47 |
* <p> Like most collection classes, this class is not synchronized. |
|
48 |
* A synchronized <tt>WeakHashMap</tt> may be constructed using the |
|
49 |
* {@link Collections#synchronizedMap Collections.synchronizedMap} |
|
50 |
* method. |
|
51 |
* |
|
52 |
* <p> This class is intended primarily for use with key objects whose |
|
53 |
* <tt>equals</tt> methods test for object identity using the |
|
54 |
* <tt>==</tt> operator. Once such a key is discarded it can never be |
|
55 |
* recreated, so it is impossible to do a lookup of that key in a |
|
56 |
* <tt>WeakHashMap</tt> at some later time and be surprised that its entry |
|
57 |
* has been removed. This class will work perfectly well with key objects |
|
58 |
* whose <tt>equals</tt> methods are not based upon object identity, such |
|
59 |
* as <tt>String</tt> instances. With such recreatable key objects, |
|
60 |
* however, the automatic removal of <tt>WeakHashMap</tt> entries whose |
|
61 |
* keys have been discarded may prove to be confusing. |
|
62 |
* |
|
63 |
* <p> The behavior of the <tt>WeakHashMap</tt> class depends in part upon |
|
64 |
* the actions of the garbage collector, so several familiar (though not |
|
65 |
* required) <tt>Map</tt> invariants do not hold for this class. Because |
|
66 |
* the garbage collector may discard keys at any time, a |
|
67 |
* <tt>WeakHashMap</tt> may behave as though an unknown thread is silently |
|
68 |
* removing entries. In particular, even if you synchronize on a |
|
69 |
* <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it |
|
70 |
* is possible for the <tt>size</tt> method to return smaller values over |
|
71 |
* time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and |
|
72 |
* then <tt>true</tt>, for the <tt>containsKey</tt> method to return |
|
73 |
* <tt>true</tt> and later <tt>false</tt> for a given key, for the |
|
74 |
* <tt>get</tt> method to return a value for a given key but later return |
|
75 |
* <tt>null</tt>, for the <tt>put</tt> method to return |
|
76 |
* <tt>null</tt> and the <tt>remove</tt> method to return |
|
77 |
* <tt>false</tt> for a key that previously appeared to be in the map, and |
|
78 |
* for successive examinations of the key set, the value collection, and |
|
79 |
* the entry set to yield successively smaller numbers of elements. |
|
80 |
* |
|
81 |
* <p> Each key object in a <tt>WeakHashMap</tt> is stored indirectly as |
|
82 |
* the referent of a weak reference. Therefore a key will automatically be |
|
83 |
* removed only after the weak references to it, both inside and outside of the |
|
84 |
* map, have been cleared by the garbage collector. |
|
85 |
* |
|
86 |
* <p> <strong>Implementation note:</strong> The value objects in a |
|
87 |
* <tt>WeakHashMap</tt> are held by ordinary strong references. Thus care |
|
88 |
* should be taken to ensure that value objects do not strongly refer to their |
|
89 |
* own keys, either directly or indirectly, since that will prevent the keys |
|
90 |
* from being discarded. Note that a value object may refer indirectly to its |
|
91 |
* key via the <tt>WeakHashMap</tt> itself; that is, a value object may |
|
92 |
* strongly refer to some other key object whose associated value object, in |
|
12864
c404d4a709de
7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents:
12859
diff
changeset
|
93 |
* turn, strongly refers to the key of the first value object. If the values |
c404d4a709de
7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents:
12859
diff
changeset
|
94 |
* in the map do not rely on the map holding strong references to them, one way |
2 | 95 |
* to deal with this is to wrap values themselves within |
96 |
* <tt>WeakReferences</tt> before |
|
97 |
* inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>, |
|
98 |
* and then unwrapping upon each <tt>get</tt>. |
|
99 |
* |
|
100 |
* <p>The iterators returned by the <tt>iterator</tt> method of the collections |
|
101 |
* returned by all of this class's "collection view methods" are |
|
102 |
* <i>fail-fast</i>: if the map is structurally modified at any time after the |
|
103 |
* iterator is created, in any way except through the iterator's own |
|
104 |
* <tt>remove</tt> method, the iterator will throw a {@link |
|
105 |
* ConcurrentModificationException}. Thus, in the face of concurrent |
|
106 |
* modification, the iterator fails quickly and cleanly, rather than risking |
|
107 |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
|
108 |
* |
|
109 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
|
110 |
* as it is, generally speaking, impossible to make any hard guarantees in the |
|
111 |
* presence of unsynchronized concurrent modification. Fail-fast iterators |
|
112 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
|
113 |
* Therefore, it would be wrong to write a program that depended on this |
|
114 |
* exception for its correctness: <i>the fail-fast behavior of iterators |
|
115 |
* should be used only to detect bugs.</i> |
|
116 |
* |
|
117 |
* <p>This class is a member of the |
|
118 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
119 |
* Java Collections Framework</a>. |
|
120 |
* |
|
121 |
* @param <K> the type of keys maintained by this map |
|
122 |
* @param <V> the type of mapped values |
|
123 |
* |
|
124 |
* @author Doug Lea |
|
125 |
* @author Josh Bloch |
|
126 |
* @author Mark Reinhold |
|
127 |
* @since 1.2 |
|
128 |
* @see java.util.HashMap |
|
129 |
* @see java.lang.ref.WeakReference |
|
130 |
*/ |
|
131 |
public class WeakHashMap<K,V> |
|
132 |
extends AbstractMap<K,V> |
|
133 |
implements Map<K,V> { |
|
134 |
||
135 |
/** |
|
136 |
* The default initial capacity -- MUST be a power of two. |
|
137 |
*/ |
|
138 |
private static final int DEFAULT_INITIAL_CAPACITY = 16; |
|
139 |
||
140 |
/** |
|
141 |
* The maximum capacity, used if a higher value is implicitly specified |
|
142 |
* by either of the constructors with arguments. |
|
143 |
* MUST be a power of two <= 1<<30. |
|
144 |
*/ |
|
145 |
private static final int MAXIMUM_CAPACITY = 1 << 30; |
|
146 |
||
147 |
/** |
|
148 |
* The load factor used when none specified in constructor. |
|
149 |
*/ |
|
150 |
private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
|
151 |
||
152 |
/** |
|
153 |
* The table, resized as necessary. Length MUST Always be a power of two. |
|
154 |
*/ |
|
155 |
Entry<K,V>[] table; |
|
156 |
||
157 |
/** |
|
158 |
* The number of key-value mappings contained in this weak hash map. |
|
159 |
*/ |
|
160 |
private int size; |
|
161 |
||
162 |
/** |
|
163 |
* The next size value at which to resize (capacity * load factor). |
|
164 |
*/ |
|
165 |
private int threshold; |
|
166 |
||
167 |
/** |
|
168 |
* The load factor for the hash table. |
|
169 |
*/ |
|
170 |
private final float loadFactor; |
|
171 |
||
172 |
/** |
|
173 |
* Reference queue for cleared WeakEntries |
|
174 |
*/ |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
175 |
private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
2 | 176 |
|
177 |
/** |
|
178 |
* The number of times this WeakHashMap has been structurally modified. |
|
179 |
* Structural modifications are those that change the number of |
|
180 |
* mappings in the map or otherwise modify its internal structure |
|
181 |
* (e.g., rehash). This field is used to make iterators on |
|
182 |
* Collection-views of the map fail-fast. |
|
183 |
* |
|
184 |
* @see ConcurrentModificationException |
|
185 |
*/ |
|
65 | 186 |
int modCount; |
2 | 187 |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
188 |
/** |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
189 |
* A randomizing value associated with this instance that is applied to |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
190 |
* hash code of keys to make hash collisions harder to find. |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
191 |
*/ |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
192 |
transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
193 |
|
2 | 194 |
@SuppressWarnings("unchecked") |
195 |
private Entry<K,V>[] newTable(int n) { |
|
12448 | 196 |
return (Entry<K,V>[]) new Entry<?,?>[n]; |
2 | 197 |
} |
198 |
||
199 |
/** |
|
200 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the given initial |
|
201 |
* capacity and the given load factor. |
|
202 |
* |
|
203 |
* @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt> |
|
204 |
* @param loadFactor The load factor of the <tt>WeakHashMap</tt> |
|
205 |
* @throws IllegalArgumentException if the initial capacity is negative, |
|
206 |
* or if the load factor is nonpositive. |
|
207 |
*/ |
|
208 |
public WeakHashMap(int initialCapacity, float loadFactor) { |
|
209 |
if (initialCapacity < 0) |
|
210 |
throw new IllegalArgumentException("Illegal Initial Capacity: "+ |
|
211 |
initialCapacity); |
|
212 |
if (initialCapacity > MAXIMUM_CAPACITY) |
|
213 |
initialCapacity = MAXIMUM_CAPACITY; |
|
214 |
||
215 |
if (loadFactor <= 0 || Float.isNaN(loadFactor)) |
|
216 |
throw new IllegalArgumentException("Illegal Load factor: "+ |
|
217 |
loadFactor); |
|
218 |
int capacity = 1; |
|
219 |
while (capacity < initialCapacity) |
|
220 |
capacity <<= 1; |
|
221 |
table = newTable(capacity); |
|
222 |
this.loadFactor = loadFactor; |
|
223 |
threshold = (int)(capacity * loadFactor); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the given initial |
|
228 |
* capacity and the default load factor (0.75). |
|
229 |
* |
|
230 |
* @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt> |
|
231 |
* @throws IllegalArgumentException if the initial capacity is negative |
|
232 |
*/ |
|
233 |
public WeakHashMap(int initialCapacity) { |
|
234 |
this(initialCapacity, DEFAULT_LOAD_FACTOR); |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the default initial |
|
239 |
* capacity (16) and load factor (0.75). |
|
240 |
*/ |
|
241 |
public WeakHashMap() { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
242 |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
2 | 243 |
} |
244 |
||
245 |
/** |
|
246 |
* Constructs a new <tt>WeakHashMap</tt> with the same mappings as the |
|
247 |
* specified map. The <tt>WeakHashMap</tt> is created with the default |
|
248 |
* load factor (0.75) and an initial capacity sufficient to hold the |
|
249 |
* mappings in the specified map. |
|
250 |
* |
|
251 |
* @param m the map whose mappings are to be placed in this map |
|
252 |
* @throws NullPointerException if the specified map is null |
|
253 |
* @since 1.3 |
|
254 |
*/ |
|
255 |
public WeakHashMap(Map<? extends K, ? extends V> m) { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
256 |
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
257 |
DEFAULT_INITIAL_CAPACITY), |
2 | 258 |
DEFAULT_LOAD_FACTOR); |
259 |
putAll(m); |
|
260 |
} |
|
261 |
||
262 |
// internal utilities |
|
263 |
||
264 |
/** |
|
265 |
* Value representing null keys inside tables. |
|
266 |
*/ |
|
267 |
private static final Object NULL_KEY = new Object(); |
|
268 |
||
269 |
/** |
|
270 |
* Use NULL_KEY for key if it is null. |
|
271 |
*/ |
|
272 |
private static Object maskNull(Object key) { |
|
273 |
return (key == null) ? NULL_KEY : key; |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Returns internal representation of null key back to caller as null. |
|
278 |
*/ |
|
279 |
static Object unmaskNull(Object key) { |
|
280 |
return (key == NULL_KEY) ? null : key; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Checks for equality of non-null reference x and possibly-null y. By |
|
285 |
* default uses Object.equals. |
|
286 |
*/ |
|
287 |
private static boolean eq(Object x, Object y) { |
|
288 |
return x == y || x.equals(y); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
292 |
* Retrieve object hash code and applies a supplemental hash function to the |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
293 |
* result hash, which defends against poor quality hash functions. This is |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
294 |
* critical because HashMap uses power-of-two length hash tables, that |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
295 |
* otherwise encounter collisions for hashCodes that do not differ |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
296 |
* in lower bits. |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
297 |
*/ |
13018 | 298 |
final int hash(Object k) { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
299 |
if (k instanceof String) { |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
300 |
return ((String) k).hash32(); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
301 |
} |
13018 | 302 |
int h = hashSeed ^ k.hashCode(); |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
303 |
|
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
304 |
// This function ensures that hashCodes that differ only by |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
305 |
// constant multiples at each bit position have a bounded |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
306 |
// number of collisions (approximately 8 at default load factor). |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
307 |
h ^= (h >>> 20) ^ (h >>> 12); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
308 |
return h ^ (h >>> 7) ^ (h >>> 4); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
309 |
} |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
310 |
|
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
311 |
/** |
2 | 312 |
* Returns index for hash code h. |
313 |
*/ |
|
314 |
private static int indexFor(int h, int length) { |
|
315 |
return h & (length-1); |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Expunges stale entries from the table. |
|
320 |
*/ |
|
321 |
private void expungeStaleEntries() { |
|
322 |
for (Object x; (x = queue.poll()) != null; ) { |
|
323 |
synchronized (queue) { |
|
324 |
@SuppressWarnings("unchecked") |
|
325 |
Entry<K,V> e = (Entry<K,V>) x; |
|
326 |
int i = indexFor(e.hash, table.length); |
|
327 |
||
328 |
Entry<K,V> prev = table[i]; |
|
329 |
Entry<K,V> p = prev; |
|
330 |
while (p != null) { |
|
331 |
Entry<K,V> next = p.next; |
|
332 |
if (p == e) { |
|
333 |
if (prev == e) |
|
334 |
table[i] = next; |
|
335 |
else |
|
336 |
prev.next = next; |
|
337 |
// Must not null out e.next; |
|
338 |
// stale entries may be in use by a HashIterator |
|
339 |
e.value = null; // Help GC |
|
340 |
size--; |
|
341 |
break; |
|
342 |
} |
|
343 |
prev = p; |
|
344 |
p = next; |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* Returns the table after first expunging stale entries. |
|
352 |
*/ |
|
353 |
private Entry<K,V>[] getTable() { |
|
354 |
expungeStaleEntries(); |
|
355 |
return table; |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Returns the number of key-value mappings in this map. |
|
360 |
* This result is a snapshot, and may not reflect unprocessed |
|
361 |
* entries that will be removed before next attempted access |
|
362 |
* because they are no longer referenced. |
|
363 |
*/ |
|
364 |
public int size() { |
|
365 |
if (size == 0) |
|
366 |
return 0; |
|
367 |
expungeStaleEntries(); |
|
368 |
return size; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
|
373 |
* This result is a snapshot, and may not reflect unprocessed |
|
374 |
* entries that will be removed before next attempted access |
|
375 |
* because they are no longer referenced. |
|
376 |
*/ |
|
377 |
public boolean isEmpty() { |
|
378 |
return size() == 0; |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Returns the value to which the specified key is mapped, |
|
383 |
* or {@code null} if this map contains no mapping for the key. |
|
384 |
* |
|
385 |
* <p>More formally, if this map contains a mapping from a key |
|
386 |
* {@code k} to a value {@code v} such that {@code (key==null ? k==null : |
|
387 |
* key.equals(k))}, then this method returns {@code v}; otherwise |
|
388 |
* it returns {@code null}. (There can be at most one such mapping.) |
|
389 |
* |
|
390 |
* <p>A return value of {@code null} does not <i>necessarily</i> |
|
391 |
* indicate that the map contains no mapping for the key; it's also |
|
392 |
* possible that the map explicitly maps the key to {@code null}. |
|
393 |
* The {@link #containsKey containsKey} operation may be used to |
|
394 |
* distinguish these two cases. |
|
395 |
* |
|
396 |
* @see #put(Object, Object) |
|
397 |
*/ |
|
398 |
public V get(Object key) { |
|
399 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
400 |
int h = hash(k); |
2 | 401 |
Entry<K,V>[] tab = getTable(); |
402 |
int index = indexFor(h, tab.length); |
|
403 |
Entry<K,V> e = tab[index]; |
|
404 |
while (e != null) { |
|
405 |
if (e.hash == h && eq(k, e.get())) |
|
406 |
return e.value; |
|
407 |
e = e.next; |
|
408 |
} |
|
409 |
return null; |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Returns <tt>true</tt> if this map contains a mapping for the |
|
414 |
* specified key. |
|
415 |
* |
|
416 |
* @param key The key whose presence in this map is to be tested |
|
417 |
* @return <tt>true</tt> if there is a mapping for <tt>key</tt>; |
|
418 |
* <tt>false</tt> otherwise |
|
419 |
*/ |
|
420 |
public boolean containsKey(Object key) { |
|
421 |
return getEntry(key) != null; |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Returns the entry associated with the specified key in this map. |
|
426 |
* Returns null if the map contains no mapping for this key. |
|
427 |
*/ |
|
428 |
Entry<K,V> getEntry(Object key) { |
|
429 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
430 |
int h = hash(k); |
2 | 431 |
Entry<K,V>[] tab = getTable(); |
432 |
int index = indexFor(h, tab.length); |
|
433 |
Entry<K,V> e = tab[index]; |
|
434 |
while (e != null && !(e.hash == h && eq(k, e.get()))) |
|
435 |
e = e.next; |
|
436 |
return e; |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Associates the specified value with the specified key in this map. |
|
441 |
* If the map previously contained a mapping for this key, the old |
|
442 |
* value is replaced. |
|
443 |
* |
|
444 |
* @param key key with which the specified value is to be associated. |
|
445 |
* @param value value to be associated with the specified key. |
|
446 |
* @return the previous value associated with <tt>key</tt>, or |
|
447 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
|
448 |
* (A <tt>null</tt> return can also indicate that the map |
|
449 |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
|
450 |
*/ |
|
451 |
public V put(K key, V value) { |
|
452 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
453 |
int h = hash(k); |
2 | 454 |
Entry<K,V>[] tab = getTable(); |
455 |
int i = indexFor(h, tab.length); |
|
456 |
||
457 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) { |
|
458 |
if (h == e.hash && eq(k, e.get())) { |
|
459 |
V oldValue = e.value; |
|
460 |
if (value != oldValue) |
|
461 |
e.value = value; |
|
462 |
return oldValue; |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
modCount++; |
|
467 |
Entry<K,V> e = tab[i]; |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
468 |
tab[i] = new Entry<>(k, value, queue, h, e); |
2 | 469 |
if (++size >= threshold) |
470 |
resize(tab.length * 2); |
|
471 |
return null; |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* Rehashes the contents of this map into a new array with a |
|
476 |
* larger capacity. This method is called automatically when the |
|
477 |
* number of keys in this map reaches its threshold. |
|
478 |
* |
|
479 |
* If current capacity is MAXIMUM_CAPACITY, this method does not |
|
480 |
* resize the map, but sets threshold to Integer.MAX_VALUE. |
|
481 |
* This has the effect of preventing future calls. |
|
482 |
* |
|
483 |
* @param newCapacity the new capacity, MUST be a power of two; |
|
484 |
* must be greater than current capacity unless current |
|
485 |
* capacity is MAXIMUM_CAPACITY (in which case value |
|
486 |
* is irrelevant). |
|
487 |
*/ |
|
488 |
void resize(int newCapacity) { |
|
489 |
Entry<K,V>[] oldTable = getTable(); |
|
490 |
int oldCapacity = oldTable.length; |
|
491 |
if (oldCapacity == MAXIMUM_CAPACITY) { |
|
492 |
threshold = Integer.MAX_VALUE; |
|
493 |
return; |
|
494 |
} |
|
495 |
||
496 |
Entry<K,V>[] newTable = newTable(newCapacity); |
|
497 |
transfer(oldTable, newTable); |
|
498 |
table = newTable; |
|
499 |
||
500 |
/* |
|
501 |
* If ignoring null elements and processing ref queue caused massive |
|
502 |
* shrinkage, then restore old table. This should be rare, but avoids |
|
503 |
* unbounded expansion of garbage-filled tables. |
|
504 |
*/ |
|
505 |
if (size >= threshold / 2) { |
|
506 |
threshold = (int)(newCapacity * loadFactor); |
|
507 |
} else { |
|
508 |
expungeStaleEntries(); |
|
509 |
transfer(newTable, oldTable); |
|
510 |
table = oldTable; |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
/** Transfers all entries from src to dest tables */ |
|
515 |
private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) { |
|
516 |
for (int j = 0; j < src.length; ++j) { |
|
517 |
Entry<K,V> e = src[j]; |
|
518 |
src[j] = null; |
|
519 |
while (e != null) { |
|
520 |
Entry<K,V> next = e.next; |
|
521 |
Object key = e.get(); |
|
522 |
if (key == null) { |
|
523 |
e.next = null; // Help GC |
|
524 |
e.value = null; // " " |
|
525 |
size--; |
|
526 |
} else { |
|
527 |
int i = indexFor(e.hash, dest.length); |
|
528 |
e.next = dest[i]; |
|
529 |
dest[i] = e; |
|
530 |
} |
|
531 |
e = next; |
|
532 |
} |
|
533 |
} |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Copies all of the mappings from the specified map to this map. |
|
538 |
* These mappings will replace any mappings that this map had for any |
|
539 |
* of the keys currently in the specified map. |
|
540 |
* |
|
541 |
* @param m mappings to be stored in this map. |
|
542 |
* @throws NullPointerException if the specified map is null. |
|
543 |
*/ |
|
544 |
public void putAll(Map<? extends K, ? extends V> m) { |
|
545 |
int numKeysToBeAdded = m.size(); |
|
546 |
if (numKeysToBeAdded == 0) |
|
547 |
return; |
|
548 |
||
549 |
/* |
|
550 |
* Expand the map if the map if the number of mappings to be added |
|
551 |
* is greater than or equal to threshold. This is conservative; the |
|
552 |
* obvious condition is (m.size() + size) >= threshold, but this |
|
553 |
* condition could result in a map with twice the appropriate capacity, |
|
554 |
* if the keys to be added overlap with the keys already in this map. |
|
555 |
* By using the conservative calculation, we subject ourself |
|
556 |
* to at most one extra resize. |
|
557 |
*/ |
|
558 |
if (numKeysToBeAdded > threshold) { |
|
559 |
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1); |
|
560 |
if (targetCapacity > MAXIMUM_CAPACITY) |
|
561 |
targetCapacity = MAXIMUM_CAPACITY; |
|
562 |
int newCapacity = table.length; |
|
563 |
while (newCapacity < targetCapacity) |
|
564 |
newCapacity <<= 1; |
|
565 |
if (newCapacity > table.length) |
|
566 |
resize(newCapacity); |
|
567 |
} |
|
568 |
||
569 |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
|
570 |
put(e.getKey(), e.getValue()); |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Removes the mapping for a key from this weak hash map if it is present. |
|
575 |
* More formally, if this map contains a mapping from key <tt>k</tt> to |
|
576 |
* value <tt>v</tt> such that <code>(key==null ? k==null : |
|
577 |
* key.equals(k))</code>, that mapping is removed. (The map can contain |
|
578 |
* at most one such mapping.) |
|
579 |
* |
|
580 |
* <p>Returns the value to which this map previously associated the key, |
|
581 |
* or <tt>null</tt> if the map contained no mapping for the key. A |
|
582 |
* return value of <tt>null</tt> does not <i>necessarily</i> indicate |
|
583 |
* that the map contained no mapping for the key; it's also possible |
|
584 |
* that the map explicitly mapped the key to <tt>null</tt>. |
|
585 |
* |
|
586 |
* <p>The map will not contain a mapping for the specified key once the |
|
587 |
* call returns. |
|
588 |
* |
|
589 |
* @param key key whose mapping is to be removed from the map |
|
590 |
* @return the previous value associated with <tt>key</tt>, or |
|
591 |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
|
592 |
*/ |
|
593 |
public V remove(Object key) { |
|
594 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
595 |
int h = hash(k); |
2 | 596 |
Entry<K,V>[] tab = getTable(); |
597 |
int i = indexFor(h, tab.length); |
|
598 |
Entry<K,V> prev = tab[i]; |
|
599 |
Entry<K,V> e = prev; |
|
600 |
||
601 |
while (e != null) { |
|
602 |
Entry<K,V> next = e.next; |
|
603 |
if (h == e.hash && eq(k, e.get())) { |
|
604 |
modCount++; |
|
605 |
size--; |
|
606 |
if (prev == e) |
|
607 |
tab[i] = next; |
|
608 |
else |
|
609 |
prev.next = next; |
|
610 |
return e.value; |
|
611 |
} |
|
612 |
prev = e; |
|
613 |
e = next; |
|
614 |
} |
|
615 |
||
616 |
return null; |
|
617 |
} |
|
618 |
||
619 |
/** Special version of remove needed by Entry set */ |
|
620 |
boolean removeMapping(Object o) { |
|
621 |
if (!(o instanceof Map.Entry)) |
|
622 |
return false; |
|
623 |
Entry<K,V>[] tab = getTable(); |
|
624 |
Map.Entry<?,?> entry = (Map.Entry<?,?>)o; |
|
625 |
Object k = maskNull(entry.getKey()); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
626 |
int h = hash(k); |
2 | 627 |
int i = indexFor(h, tab.length); |
628 |
Entry<K,V> prev = tab[i]; |
|
629 |
Entry<K,V> e = prev; |
|
630 |
||
631 |
while (e != null) { |
|
632 |
Entry<K,V> next = e.next; |
|
633 |
if (h == e.hash && e.equals(entry)) { |
|
634 |
modCount++; |
|
635 |
size--; |
|
636 |
if (prev == e) |
|
637 |
tab[i] = next; |
|
638 |
else |
|
639 |
prev.next = next; |
|
640 |
return true; |
|
641 |
} |
|
642 |
prev = e; |
|
643 |
e = next; |
|
644 |
} |
|
645 |
||
646 |
return false; |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* Removes all of the mappings from this map. |
|
651 |
* The map will be empty after this call returns. |
|
652 |
*/ |
|
653 |
public void clear() { |
|
654 |
// clear out ref queue. We don't need to expunge entries |
|
655 |
// since table is getting cleared. |
|
656 |
while (queue.poll() != null) |
|
657 |
; |
|
658 |
||
659 |
modCount++; |
|
660 |
Arrays.fill(table, null); |
|
661 |
size = 0; |
|
662 |
||
663 |
// Allocation of array may have caused GC, which may have caused |
|
664 |
// additional entries to go stale. Removing these entries from the |
|
665 |
// reference queue will make them eligible for reclamation. |
|
666 |
while (queue.poll() != null) |
|
667 |
; |
|
668 |
} |
|
669 |
||
670 |
/** |
|
671 |
* Returns <tt>true</tt> if this map maps one or more keys to the |
|
672 |
* specified value. |
|
673 |
* |
|
674 |
* @param value value whose presence in this map is to be tested |
|
675 |
* @return <tt>true</tt> if this map maps one or more keys to the |
|
676 |
* specified value |
|
677 |
*/ |
|
678 |
public boolean containsValue(Object value) { |
|
679 |
if (value==null) |
|
680 |
return containsNullValue(); |
|
681 |
||
682 |
Entry<K,V>[] tab = getTable(); |
|
683 |
for (int i = tab.length; i-- > 0;) |
|
684 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
|
685 |
if (value.equals(e.value)) |
|
686 |
return true; |
|
687 |
return false; |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Special-case code for containsValue with null argument |
|
692 |
*/ |
|
693 |
private boolean containsNullValue() { |
|
694 |
Entry<K,V>[] tab = getTable(); |
|
695 |
for (int i = tab.length; i-- > 0;) |
|
696 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
|
697 |
if (e.value==null) |
|
698 |
return true; |
|
699 |
return false; |
|
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* The entries in this hash table extend WeakReference, using its main ref |
|
704 |
* field as the key. |
|
705 |
*/ |
|
706 |
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { |
|
707 |
V value; |
|
708 |
final int hash; |
|
709 |
Entry<K,V> next; |
|
710 |
||
711 |
/** |
|
712 |
* Creates new entry. |
|
713 |
*/ |
|
714 |
Entry(Object key, V value, |
|
715 |
ReferenceQueue<Object> queue, |
|
716 |
int hash, Entry<K,V> next) { |
|
717 |
super(key, queue); |
|
718 |
this.value = value; |
|
719 |
this.hash = hash; |
|
720 |
this.next = next; |
|
721 |
} |
|
722 |
||
723 |
@SuppressWarnings("unchecked") |
|
724 |
public K getKey() { |
|
725 |
return (K) WeakHashMap.unmaskNull(get()); |
|
726 |
} |
|
727 |
||
728 |
public V getValue() { |
|
729 |
return value; |
|
730 |
} |
|
731 |
||
732 |
public V setValue(V newValue) { |
|
733 |
V oldValue = value; |
|
734 |
value = newValue; |
|
735 |
return oldValue; |
|
736 |
} |
|
737 |
||
738 |
public boolean equals(Object o) { |
|
739 |
if (!(o instanceof Map.Entry)) |
|
740 |
return false; |
|
741 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
|
742 |
K k1 = getKey(); |
|
743 |
Object k2 = e.getKey(); |
|
744 |
if (k1 == k2 || (k1 != null && k1.equals(k2))) { |
|
745 |
V v1 = getValue(); |
|
746 |
Object v2 = e.getValue(); |
|
747 |
if (v1 == v2 || (v1 != null && v1.equals(v2))) |
|
748 |
return true; |
|
749 |
} |
|
750 |
return false; |
|
751 |
} |
|
752 |
||
753 |
public int hashCode() { |
|
754 |
K k = getKey(); |
|
755 |
V v = getValue(); |
|
756 |
return ((k==null ? 0 : k.hashCode()) ^ |
|
757 |
(v==null ? 0 : v.hashCode())); |
|
758 |
} |
|
759 |
||
760 |
public String toString() { |
|
761 |
return getKey() + "=" + getValue(); |
|
762 |
} |
|
763 |
} |
|
764 |
||
765 |
private abstract class HashIterator<T> implements Iterator<T> { |
|
766 |
private int index; |
|
767 |
private Entry<K,V> entry = null; |
|
768 |
private Entry<K,V> lastReturned = null; |
|
769 |
private int expectedModCount = modCount; |
|
770 |
||
771 |
/** |
|
772 |
* Strong reference needed to avoid disappearance of key |
|
773 |
* between hasNext and next |
|
774 |
*/ |
|
775 |
private Object nextKey = null; |
|
776 |
||
777 |
/** |
|
778 |
* Strong reference needed to avoid disappearance of key |
|
779 |
* between nextEntry() and any use of the entry |
|
780 |
*/ |
|
781 |
private Object currentKey = null; |
|
782 |
||
783 |
HashIterator() { |
|
784 |
index = isEmpty() ? 0 : table.length; |
|
785 |
} |
|
786 |
||
787 |
public boolean hasNext() { |
|
788 |
Entry<K,V>[] t = table; |
|
789 |
||
790 |
while (nextKey == null) { |
|
791 |
Entry<K,V> e = entry; |
|
792 |
int i = index; |
|
793 |
while (e == null && i > 0) |
|
794 |
e = t[--i]; |
|
795 |
entry = e; |
|
796 |
index = i; |
|
797 |
if (e == null) { |
|
798 |
currentKey = null; |
|
799 |
return false; |
|
800 |
} |
|
801 |
nextKey = e.get(); // hold on to key in strong ref |
|
802 |
if (nextKey == null) |
|
803 |
entry = entry.next; |
|
804 |
} |
|
805 |
return true; |
|
806 |
} |
|
807 |
||
808 |
/** The common parts of next() across different types of iterators */ |
|
809 |
protected Entry<K,V> nextEntry() { |
|
810 |
if (modCount != expectedModCount) |
|
811 |
throw new ConcurrentModificationException(); |
|
812 |
if (nextKey == null && !hasNext()) |
|
813 |
throw new NoSuchElementException(); |
|
814 |
||
815 |
lastReturned = entry; |
|
816 |
entry = entry.next; |
|
817 |
currentKey = nextKey; |
|
818 |
nextKey = null; |
|
819 |
return lastReturned; |
|
820 |
} |
|
821 |
||
822 |
public void remove() { |
|
823 |
if (lastReturned == null) |
|
824 |
throw new IllegalStateException(); |
|
825 |
if (modCount != expectedModCount) |
|
826 |
throw new ConcurrentModificationException(); |
|
827 |
||
828 |
WeakHashMap.this.remove(currentKey); |
|
829 |
expectedModCount = modCount; |
|
830 |
lastReturned = null; |
|
831 |
currentKey = null; |
|
832 |
} |
|
833 |
||
834 |
} |
|
835 |
||
836 |
private class ValueIterator extends HashIterator<V> { |
|
837 |
public V next() { |
|
838 |
return nextEntry().value; |
|
839 |
} |
|
840 |
} |
|
841 |
||
842 |
private class KeyIterator extends HashIterator<K> { |
|
843 |
public K next() { |
|
844 |
return nextEntry().getKey(); |
|
845 |
} |
|
846 |
} |
|
847 |
||
848 |
private class EntryIterator extends HashIterator<Map.Entry<K,V>> { |
|
849 |
public Map.Entry<K,V> next() { |
|
850 |
return nextEntry(); |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
// Views |
|
855 |
||
856 |
private transient Set<Map.Entry<K,V>> entrySet = null; |
|
857 |
||
858 |
/** |
|
859 |
* Returns a {@link Set} view of the keys contained in this map. |
|
860 |
* The set is backed by the map, so changes to the map are |
|
861 |
* reflected in the set, and vice-versa. If the map is modified |
|
862 |
* while an iteration over the set is in progress (except through |
|
863 |
* the iterator's own <tt>remove</tt> operation), the results of |
|
864 |
* the iteration are undefined. The set supports element removal, |
|
865 |
* which removes the corresponding mapping from the map, via the |
|
866 |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
|
867 |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
|
868 |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
|
869 |
* operations. |
|
870 |
*/ |
|
871 |
public Set<K> keySet() { |
|
872 |
Set<K> ks = keySet; |
|
873 |
return (ks != null ? ks : (keySet = new KeySet())); |
|
874 |
} |
|
875 |
||
876 |
private class KeySet extends AbstractSet<K> { |
|
877 |
public Iterator<K> iterator() { |
|
878 |
return new KeyIterator(); |
|
879 |
} |
|
880 |
||
881 |
public int size() { |
|
882 |
return WeakHashMap.this.size(); |
|
883 |
} |
|
884 |
||
885 |
public boolean contains(Object o) { |
|
886 |
return containsKey(o); |
|
887 |
} |
|
888 |
||
889 |
public boolean remove(Object o) { |
|
890 |
if (containsKey(o)) { |
|
891 |
WeakHashMap.this.remove(o); |
|
892 |
return true; |
|
893 |
} |
|
894 |
else |
|
895 |
return false; |
|
896 |
} |
|
897 |
||
898 |
public void clear() { |
|
899 |
WeakHashMap.this.clear(); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
/** |
|
904 |
* Returns a {@link Collection} view of the values contained in this map. |
|
905 |
* The collection is backed by the map, so changes to the map are |
|
906 |
* reflected in the collection, and vice-versa. If the map is |
|
907 |
* modified while an iteration over the collection is in progress |
|
908 |
* (except through the iterator's own <tt>remove</tt> operation), |
|
909 |
* the results of the iteration are undefined. The collection |
|
910 |
* supports element removal, which removes the corresponding |
|
911 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
912 |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
|
913 |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
|
914 |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
|
915 |
*/ |
|
916 |
public Collection<V> values() { |
|
917 |
Collection<V> vs = values; |
|
918 |
return (vs != null) ? vs : (values = new Values()); |
|
919 |
} |
|
920 |
||
921 |
private class Values extends AbstractCollection<V> { |
|
922 |
public Iterator<V> iterator() { |
|
923 |
return new ValueIterator(); |
|
924 |
} |
|
925 |
||
926 |
public int size() { |
|
927 |
return WeakHashMap.this.size(); |
|
928 |
} |
|
929 |
||
930 |
public boolean contains(Object o) { |
|
931 |
return containsValue(o); |
|
932 |
} |
|
933 |
||
934 |
public void clear() { |
|
935 |
WeakHashMap.this.clear(); |
|
936 |
} |
|
937 |
} |
|
938 |
||
939 |
/** |
|
940 |
* Returns a {@link Set} view of the mappings contained in this map. |
|
941 |
* The set is backed by the map, so changes to the map are |
|
942 |
* reflected in the set, and vice-versa. If the map is modified |
|
943 |
* while an iteration over the set is in progress (except through |
|
944 |
* the iterator's own <tt>remove</tt> operation, or through the |
|
945 |
* <tt>setValue</tt> operation on a map entry returned by the |
|
946 |
* iterator) the results of the iteration are undefined. The set |
|
947 |
* supports element removal, which removes the corresponding |
|
948 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
949 |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and |
|
950 |
* <tt>clear</tt> operations. It does not support the |
|
951 |
* <tt>add</tt> or <tt>addAll</tt> operations. |
|
952 |
*/ |
|
953 |
public Set<Map.Entry<K,V>> entrySet() { |
|
954 |
Set<Map.Entry<K,V>> es = entrySet; |
|
955 |
return es != null ? es : (entrySet = new EntrySet()); |
|
956 |
} |
|
957 |
||
958 |
private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
|
959 |
public Iterator<Map.Entry<K,V>> iterator() { |
|
960 |
return new EntryIterator(); |
|
961 |
} |
|
962 |
||
963 |
public boolean contains(Object o) { |
|
964 |
if (!(o instanceof Map.Entry)) |
|
965 |
return false; |
|
966 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
|
967 |
Entry<K,V> candidate = getEntry(e.getKey()); |
|
968 |
return candidate != null && candidate.equals(e); |
|
969 |
} |
|
970 |
||
971 |
public boolean remove(Object o) { |
|
972 |
return removeMapping(o); |
|
973 |
} |
|
974 |
||
975 |
public int size() { |
|
976 |
return WeakHashMap.this.size(); |
|
977 |
} |
|
978 |
||
979 |
public void clear() { |
|
980 |
WeakHashMap.this.clear(); |
|
981 |
} |
|
982 |
||
983 |
private List<Map.Entry<K,V>> deepCopy() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
984 |
List<Map.Entry<K,V>> list = new ArrayList<>(size()); |
2 | 985 |
for (Map.Entry<K,V> e : this) |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
986 |
list.add(new AbstractMap.SimpleEntry<>(e)); |
2 | 987 |
return list; |
988 |
} |
|
989 |
||
990 |
public Object[] toArray() { |
|
991 |
return deepCopy().toArray(); |
|
992 |
} |
|
993 |
||
994 |
public <T> T[] toArray(T[] a) { |
|
995 |
return deepCopy().toArray(a); |
|
996 |
} |
|
997 |
} |
|
998 |
} |