author | psandoz |
Tue, 06 Aug 2013 14:26:34 +0100 | |
changeset 19435 | 9d7530ff42cb |
parent 18280 | 6c3c0ff49eb5 |
child 19806 | dda89341ee2d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12859
diff
changeset
|
2 |
* Copyright (c) 2000, 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.*; |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
28 |
import java.util.function.BiConsumer; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
29 |
import java.util.function.BiFunction; |
2 | 30 |
|
31 |
/** |
|
32 |
* <p>Hash table and linked list implementation of the <tt>Map</tt> interface, |
|
33 |
* with predictable iteration order. This implementation differs from |
|
34 |
* <tt>HashMap</tt> in that it maintains a doubly-linked list running through |
|
35 |
* all of its entries. This linked list defines the iteration ordering, |
|
36 |
* which is normally the order in which keys were inserted into the map |
|
37 |
* (<i>insertion-order</i>). Note that insertion order is not affected |
|
38 |
* if a key is <i>re-inserted</i> into the map. (A key <tt>k</tt> is |
|
39 |
* reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when |
|
40 |
* <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to |
|
41 |
* the invocation.) |
|
42 |
* |
|
43 |
* <p>This implementation spares its clients from the unspecified, generally |
|
44 |
* chaotic ordering provided by {@link HashMap} (and {@link Hashtable}), |
|
45 |
* without incurring the increased cost associated with {@link TreeMap}. It |
|
46 |
* can be used to produce a copy of a map that has the same order as the |
|
47 |
* original, regardless of the original map's implementation: |
|
48 |
* <pre> |
|
49 |
* void foo(Map m) { |
|
50 |
* Map copy = new LinkedHashMap(m); |
|
51 |
* ... |
|
52 |
* } |
|
53 |
* </pre> |
|
54 |
* This technique is particularly useful if a module takes a map on input, |
|
55 |
* copies it, and later returns results whose order is determined by that of |
|
56 |
* the copy. (Clients generally appreciate having things returned in the same |
|
57 |
* order they were presented.) |
|
58 |
* |
|
59 |
* <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
60 |
* provided to create a <tt>LinkedHashMap</tt> whose order of iteration is the |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
61 |
* order in which its entries were last accessed, from least-recently accessed |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
62 |
* to most-recently (<i>access-order</i>). This kind of map is well-suited to |
2 | 63 |
* building LRU caches. Invoking the <tt>put</tt> or <tt>get</tt> method |
64 |
* results in an access to the corresponding entry (assuming it exists after |
|
65 |
* the invocation completes). The <tt>putAll</tt> method generates one entry |
|
66 |
* access for each mapping in the specified map, in the order that key-value |
|
67 |
* mappings are provided by the specified map's entry set iterator. <i>No |
|
68 |
* other methods generate entry accesses.</i> In particular, operations on |
|
69 |
* collection-views do <i>not</i> affect the order of iteration of the backing |
|
70 |
* map. |
|
71 |
* |
|
72 |
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to |
|
73 |
* impose a policy for removing stale mappings automatically when new mappings |
|
74 |
* are added to the map. |
|
75 |
* |
|
76 |
* <p>This class provides all of the optional <tt>Map</tt> operations, and |
|
77 |
* permits null elements. Like <tt>HashMap</tt>, it provides constant-time |
|
78 |
* performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and |
|
79 |
* <tt>remove</tt>), assuming the hash function disperses elements |
|
80 |
* properly among the buckets. Performance is likely to be just slightly |
|
81 |
* below that of <tt>HashMap</tt>, due to the added expense of maintaining the |
|
82 |
* linked list, with one exception: Iteration over the collection-views |
|
83 |
* of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i> |
|
84 |
* of the map, regardless of its capacity. Iteration over a <tt>HashMap</tt> |
|
85 |
* is likely to be more expensive, requiring time proportional to its |
|
86 |
* <i>capacity</i>. |
|
87 |
* |
|
88 |
* <p>A linked hash map has two parameters that affect its performance: |
|
89 |
* <i>initial capacity</i> and <i>load factor</i>. They are defined precisely |
|
90 |
* as for <tt>HashMap</tt>. Note, however, that the penalty for choosing an |
|
91 |
* excessively high value for initial capacity is less severe for this class |
|
92 |
* than for <tt>HashMap</tt>, as iteration times for this class are unaffected |
|
93 |
* by capacity. |
|
94 |
* |
|
95 |
* <p><strong>Note that this implementation is not synchronized.</strong> |
|
96 |
* If multiple threads access a linked hash map concurrently, and at least |
|
97 |
* one of the threads modifies the map structurally, it <em>must</em> be |
|
98 |
* synchronized externally. This is typically accomplished by |
|
99 |
* synchronizing on some object that naturally encapsulates the map. |
|
100 |
* |
|
101 |
* If no such object exists, the map should be "wrapped" using the |
|
102 |
* {@link Collections#synchronizedMap Collections.synchronizedMap} |
|
103 |
* method. This is best done at creation time, to prevent accidental |
|
104 |
* unsynchronized access to the map:<pre> |
|
105 |
* Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre> |
|
106 |
* |
|
107 |
* A structural modification is any operation that adds or deletes one or more |
|
108 |
* mappings or, in the case of access-ordered linked hash maps, affects |
|
109 |
* iteration order. In insertion-ordered linked hash maps, merely changing |
|
110 |
* the value associated with a key that is already contained in the map is not |
|
111 |
* a structural modification. <strong>In access-ordered linked hash maps, |
|
112 |
* merely querying the map with <tt>get</tt> is a structural |
|
113 |
* modification.</strong>) |
|
114 |
* |
|
115 |
* <p>The iterators returned by the <tt>iterator</tt> method of the collections |
|
116 |
* returned by all of this class's collection view methods are |
|
117 |
* <em>fail-fast</em>: if the map is structurally modified at any time after |
|
118 |
* the iterator is created, in any way except through the iterator's own |
|
119 |
* <tt>remove</tt> method, the iterator will throw a {@link |
|
120 |
* ConcurrentModificationException}. Thus, in the face of concurrent |
|
121 |
* modification, the iterator fails quickly and cleanly, rather than risking |
|
122 |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
|
123 |
* |
|
124 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
|
125 |
* as it is, generally speaking, impossible to make any hard guarantees in the |
|
126 |
* presence of unsynchronized concurrent modification. Fail-fast iterators |
|
127 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
|
128 |
* Therefore, it would be wrong to write a program that depended on this |
|
129 |
* exception for its correctness: <i>the fail-fast behavior of iterators |
|
130 |
* should be used only to detect bugs.</i> |
|
131 |
* |
|
19435
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
132 |
* <p>The spliterators returned by the spliterator method of the collections |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
133 |
* returned by all of this class's collection view methods are |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
134 |
* <em><a href="Spliterator.html#binding">late-binding</a></em>, |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
135 |
* <em>fail-fast</em>, and additionally report {@link Spliterator#ORDERED}. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
136 |
* |
2 | 137 |
* <p>This class is a member of the |
138 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
139 |
* Java Collections Framework</a>. |
|
140 |
* |
|
19435
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
141 |
* @implNote |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
142 |
* The spliterators returned by the spliterator method of the collections |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
143 |
* returned by all of this class's collection view methods are created from |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
144 |
* the iterators of the corresponding collections. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
18280
diff
changeset
|
145 |
* |
2 | 146 |
* @param <K> the type of keys maintained by this map |
147 |
* @param <V> the type of mapped values |
|
148 |
* |
|
149 |
* @author Josh Bloch |
|
150 |
* @see Object#hashCode() |
|
151 |
* @see Collection |
|
152 |
* @see Map |
|
153 |
* @see HashMap |
|
154 |
* @see TreeMap |
|
155 |
* @see Hashtable |
|
156 |
* @since 1.4 |
|
157 |
*/ |
|
158 |
||
159 |
public class LinkedHashMap<K,V> |
|
160 |
extends HashMap<K,V> |
|
161 |
implements Map<K,V> |
|
162 |
{ |
|
163 |
||
164 |
private static final long serialVersionUID = 3801124242820219131L; |
|
165 |
||
166 |
/** |
|
167 |
* The head of the doubly linked list. |
|
168 |
*/ |
|
169 |
private transient Entry<K,V> header; |
|
170 |
||
171 |
/** |
|
172 |
* The iteration ordering method for this linked hash map: <tt>true</tt> |
|
173 |
* for access-order, <tt>false</tt> for insertion-order. |
|
174 |
* |
|
175 |
* @serial |
|
176 |
*/ |
|
177 |
private final boolean accessOrder; |
|
178 |
||
179 |
/** |
|
180 |
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance |
|
181 |
* with the specified initial capacity and load factor. |
|
182 |
* |
|
183 |
* @param initialCapacity the initial capacity |
|
184 |
* @param loadFactor the load factor |
|
185 |
* @throws IllegalArgumentException if the initial capacity is negative |
|
186 |
* or the load factor is nonpositive |
|
187 |
*/ |
|
188 |
public LinkedHashMap(int initialCapacity, float loadFactor) { |
|
189 |
super(initialCapacity, loadFactor); |
|
190 |
accessOrder = false; |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance |
|
195 |
* with the specified initial capacity and a default load factor (0.75). |
|
196 |
* |
|
197 |
* @param initialCapacity the initial capacity |
|
198 |
* @throws IllegalArgumentException if the initial capacity is negative |
|
199 |
*/ |
|
200 |
public LinkedHashMap(int initialCapacity) { |
|
201 |
super(initialCapacity); |
|
202 |
accessOrder = false; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance |
|
207 |
* with the default initial capacity (16) and load factor (0.75). |
|
208 |
*/ |
|
209 |
public LinkedHashMap() { |
|
210 |
super(); |
|
211 |
accessOrder = false; |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with |
|
216 |
* the same mappings as the specified map. The <tt>LinkedHashMap</tt> |
|
217 |
* instance is created with a default load factor (0.75) and an initial |
|
218 |
* capacity sufficient to hold the mappings in the specified map. |
|
219 |
* |
|
220 |
* @param m the map whose mappings are to be placed in this map |
|
221 |
* @throws NullPointerException if the specified map is null |
|
222 |
*/ |
|
223 |
public LinkedHashMap(Map<? extends K, ? extends V> m) { |
|
224 |
super(m); |
|
225 |
accessOrder = false; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Constructs an empty <tt>LinkedHashMap</tt> instance with the |
|
230 |
* specified initial capacity, load factor and ordering mode. |
|
231 |
* |
|
232 |
* @param initialCapacity the initial capacity |
|
233 |
* @param loadFactor the load factor |
|
234 |
* @param accessOrder the ordering mode - <tt>true</tt> for |
|
235 |
* access-order, <tt>false</tt> for insertion-order |
|
236 |
* @throws IllegalArgumentException if the initial capacity is negative |
|
237 |
* or the load factor is nonpositive |
|
238 |
*/ |
|
239 |
public LinkedHashMap(int initialCapacity, |
|
240 |
float loadFactor, |
|
241 |
boolean accessOrder) { |
|
242 |
super(initialCapacity, loadFactor); |
|
243 |
this.accessOrder = accessOrder; |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Called by superclass constructors and pseudoconstructors (clone, |
|
248 |
* readObject) before any entries are inserted into the map. Initializes |
|
249 |
* the chain. |
|
250 |
*/ |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
251 |
@Override |
2 | 252 |
void init() { |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
253 |
header = new Entry<>(-1, null, null, null); |
2 | 254 |
header.before = header.after = header; |
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Returns <tt>true</tt> if this map maps one or more keys to the |
|
259 |
* specified value. |
|
260 |
* |
|
261 |
* @param value value whose presence in this map is to be tested |
|
262 |
* @return <tt>true</tt> if this map maps one or more keys to the |
|
263 |
* specified value |
|
264 |
*/ |
|
265 |
public boolean containsValue(Object value) { |
|
266 |
// Overridden to take advantage of faster iterator |
|
267 |
if (value==null) { |
|
12448 | 268 |
for (Entry<?,?> e = header.after; e != header; e = e.after) |
2 | 269 |
if (e.value==null) |
270 |
return true; |
|
271 |
} else { |
|
12448 | 272 |
for (Entry<?,?> e = header.after; e != header; e = e.after) |
2 | 273 |
if (value.equals(e.value)) |
274 |
return true; |
|
275 |
} |
|
276 |
return false; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Returns the value to which the specified key is mapped, |
|
281 |
* or {@code null} if this map contains no mapping for the key. |
|
282 |
* |
|
283 |
* <p>More formally, if this map contains a mapping from a key |
|
284 |
* {@code k} to a value {@code v} such that {@code (key==null ? k==null : |
|
285 |
* key.equals(k))}, then this method returns {@code v}; otherwise |
|
286 |
* it returns {@code null}. (There can be at most one such mapping.) |
|
287 |
* |
|
288 |
* <p>A return value of {@code null} does not <i>necessarily</i> |
|
289 |
* indicate that the map contains no mapping for the key; it's also |
|
290 |
* possible that the map explicitly maps the key to {@code null}. |
|
291 |
* The {@link #containsKey containsKey} operation may be used to |
|
292 |
* distinguish these two cases. |
|
293 |
*/ |
|
294 |
public V get(Object key) { |
|
295 |
Entry<K,V> e = (Entry<K,V>)getEntry(key); |
|
296 |
if (e == null) |
|
297 |
return null; |
|
298 |
e.recordAccess(this); |
|
299 |
return e.value; |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Removes all of the mappings from this map. |
|
304 |
* The map will be empty after this call returns. |
|
305 |
*/ |
|
306 |
public void clear() { |
|
307 |
super.clear(); |
|
308 |
header.before = header.after = header; |
|
309 |
} |
|
310 |
||
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
311 |
@Override |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
312 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
313 |
Objects.requireNonNull(action); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
314 |
int expectedModCount = modCount; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
315 |
for (Entry<K, V> entry = header.after; entry != header; entry = entry.after) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
316 |
action.accept(entry.key, entry.value); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
317 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
318 |
if (expectedModCount != modCount) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
319 |
throw new ConcurrentModificationException(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
320 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
321 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
322 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
323 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
324 |
@Override |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
325 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
326 |
Objects.requireNonNull(function); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
327 |
int expectedModCount = modCount; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
328 |
for (Entry<K, V> entry = header.after; entry != header; entry = entry.after) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
329 |
entry.value = function.apply(entry.key, entry.value); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
330 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
331 |
if (expectedModCount != modCount) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
332 |
throw new ConcurrentModificationException(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
333 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
334 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
335 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18156
diff
changeset
|
336 |
|
2 | 337 |
/** |
338 |
* LinkedHashMap entry. |
|
339 |
*/ |
|
340 |
private static class Entry<K,V> extends HashMap.Entry<K,V> { |
|
341 |
// These fields comprise the doubly linked list used for iteration. |
|
342 |
Entry<K,V> before, after; |
|
343 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
344 |
Entry(int hash, K key, V value, Object next) { |
2 | 345 |
super(hash, key, value, next); |
346 |
} |
|
347 |
||
348 |
/** |
|
349 |
* Removes this entry from the linked list. |
|
350 |
*/ |
|
351 |
private void remove() { |
|
352 |
before.after = after; |
|
353 |
after.before = before; |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Inserts this entry before the specified existing entry in the list. |
|
358 |
*/ |
|
359 |
private void addBefore(Entry<K,V> existingEntry) { |
|
360 |
after = existingEntry; |
|
361 |
before = existingEntry.before; |
|
362 |
before.after = this; |
|
363 |
after.before = this; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* This method is invoked by the superclass whenever the value |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
368 |
* of a pre-existing entry is read by Map.get or modified by Map.put. |
2 | 369 |
* If the enclosing Map is access-ordered, it moves the entry |
370 |
* to the end of the list; otherwise, it does nothing. |
|
371 |
*/ |
|
372 |
void recordAccess(HashMap<K,V> m) { |
|
373 |
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m; |
|
374 |
if (lm.accessOrder) { |
|
375 |
lm.modCount++; |
|
376 |
remove(); |
|
377 |
addBefore(lm.header); |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
void recordRemoval(HashMap<K,V> m) { |
|
382 |
remove(); |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
private abstract class LinkedHashIterator<T> implements Iterator<T> { |
|
387 |
Entry<K,V> nextEntry = header.after; |
|
388 |
Entry<K,V> lastReturned = null; |
|
389 |
||
390 |
/** |
|
391 |
* The modCount value that the iterator believes that the backing |
|
392 |
* List should have. If this expectation is violated, the iterator |
|
393 |
* has detected concurrent modification. |
|
394 |
*/ |
|
395 |
int expectedModCount = modCount; |
|
396 |
||
397 |
public boolean hasNext() { |
|
398 |
return nextEntry != header; |
|
399 |
} |
|
400 |
||
401 |
public void remove() { |
|
402 |
if (lastReturned == null) |
|
403 |
throw new IllegalStateException(); |
|
404 |
if (modCount != expectedModCount) |
|
405 |
throw new ConcurrentModificationException(); |
|
406 |
||
407 |
LinkedHashMap.this.remove(lastReturned.key); |
|
408 |
lastReturned = null; |
|
409 |
expectedModCount = modCount; |
|
410 |
} |
|
411 |
||
412 |
Entry<K,V> nextEntry() { |
|
413 |
if (modCount != expectedModCount) |
|
414 |
throw new ConcurrentModificationException(); |
|
415 |
if (nextEntry == header) |
|
416 |
throw new NoSuchElementException(); |
|
417 |
||
418 |
Entry<K,V> e = lastReturned = nextEntry; |
|
419 |
nextEntry = e.after; |
|
420 |
return e; |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
private class KeyIterator extends LinkedHashIterator<K> { |
|
425 |
public K next() { return nextEntry().getKey(); } |
|
426 |
} |
|
427 |
||
428 |
private class ValueIterator extends LinkedHashIterator<V> { |
|
429 |
public V next() { return nextEntry().value; } |
|
430 |
} |
|
431 |
||
432 |
private class EntryIterator extends LinkedHashIterator<Map.Entry<K,V>> { |
|
433 |
public Map.Entry<K,V> next() { return nextEntry(); } |
|
434 |
} |
|
435 |
||
436 |
// These Overrides alter the behavior of superclass view iterator() methods |
|
437 |
Iterator<K> newKeyIterator() { return new KeyIterator(); } |
|
438 |
Iterator<V> newValueIterator() { return new ValueIterator(); } |
|
439 |
Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); } |
|
440 |
||
441 |
/** |
|
442 |
* This override alters behavior of superclass put method. It causes newly |
|
443 |
* allocated entry to get inserted at the end of the linked list and |
|
444 |
* removes the eldest entry if appropriate. |
|
445 |
*/ |
|
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
446 |
@Override |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
447 |
void addEntry(int hash, K key, V value, int bucketIndex, boolean checkIfNeedTree) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
448 |
super.addEntry(hash, key, value, bucketIndex, checkIfNeedTree); |
2 | 449 |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
450 |
// Remove eldest entry if instructed |
2 | 451 |
Entry<K,V> eldest = header.after; |
452 |
if (removeEldestEntry(eldest)) { |
|
453 |
removeEntryForKey(eldest.key); |
|
454 |
} |
|
455 |
} |
|
456 |
||
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
457 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
458 |
* Create a new LinkedHashMap.Entry and setup the before/after pointers |
2 | 459 |
*/ |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
460 |
@Override |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
461 |
HashMap.Entry<K,V> newEntry(int hash, K key, V value, Object next) { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
462 |
Entry<K,V> newEntry = new Entry<>(hash, key, value, next); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
463 |
newEntry.addBefore(header); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
14342
diff
changeset
|
464 |
return newEntry; |
2 | 465 |
} |
466 |
||
467 |
/** |
|
468 |
* Returns <tt>true</tt> if this map should remove its eldest entry. |
|
469 |
* This method is invoked by <tt>put</tt> and <tt>putAll</tt> after |
|
470 |
* inserting a new entry into the map. It provides the implementor |
|
471 |
* with the opportunity to remove the eldest entry each time a new one |
|
472 |
* is added. This is useful if the map represents a cache: it allows |
|
473 |
* the map to reduce memory consumption by deleting stale entries. |
|
474 |
* |
|
475 |
* <p>Sample use: this override will allow the map to grow up to 100 |
|
476 |
* entries and then delete the eldest entry each time a new entry is |
|
477 |
* added, maintaining a steady state of 100 entries. |
|
18156 | 478 |
* <pre>{@code |
2 | 479 |
* private static final int MAX_ENTRIES = 100; |
480 |
* |
|
481 |
* protected boolean removeEldestEntry(Map.Entry eldest) { |
|
482 |
* return size() > MAX_ENTRIES; |
|
483 |
* } |
|
18156 | 484 |
* }</pre> |
2 | 485 |
* |
486 |
* <p>This method typically does not modify the map in any way, |
|
487 |
* instead allowing the map to modify itself as directed by its |
|
488 |
* return value. It <i>is</i> permitted for this method to modify |
|
489 |
* the map directly, but if it does so, it <i>must</i> return |
|
490 |
* <tt>false</tt> (indicating that the map should not attempt any |
|
491 |
* further modification). The effects of returning <tt>true</tt> |
|
492 |
* after modifying the map from within this method are unspecified. |
|
493 |
* |
|
494 |
* <p>This implementation merely returns <tt>false</tt> (so that this |
|
495 |
* map acts like a normal map - the eldest element is never removed). |
|
496 |
* |
|
497 |
* @param eldest The least recently inserted entry in the map, or if |
|
498 |
* this is an access-ordered map, the least recently accessed |
|
499 |
* entry. This is the entry that will be removed it this |
|
500 |
* method returns <tt>true</tt>. If the map was empty prior |
|
501 |
* to the <tt>put</tt> or <tt>putAll</tt> invocation resulting |
|
502 |
* in this invocation, this will be the entry that was just |
|
503 |
* inserted; in other words, if the map contains a single |
|
504 |
* entry, the eldest entry is also the newest. |
|
505 |
* @return <tt>true</tt> if the eldest entry should be removed |
|
506 |
* from the map; <tt>false</tt> if it should be retained. |
|
507 |
*/ |
|
508 |
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { |
|
509 |
return false; |
|
510 |
} |
|
511 |
} |