author | mduigou |
Fri, 13 Dec 2013 13:34:55 -0800 | |
changeset 22055 | d9836bf9992a |
parent 21352 | 0372edc9a995 |
child 23738 | 098d1470822e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16867 | 2 |
* Copyright (c) 1997, 2013, 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 |
||
16867 | 28 |
import java.util.function.BiConsumer; |
29 |
import java.util.function.BiFunction; |
|
30 |
import java.util.function.Function; |
|
18571 | 31 |
import java.io.Serializable; |
16867 | 32 |
|
2 | 33 |
/** |
34 |
* An object that maps keys to values. A map cannot contain duplicate keys; |
|
35 |
* each key can map to at most one value. |
|
36 |
* |
|
37 |
* <p>This interface takes the place of the <tt>Dictionary</tt> class, which |
|
38 |
* was a totally abstract class rather than an interface. |
|
39 |
* |
|
40 |
* <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which |
|
41 |
* allow a map's contents to be viewed as a set of keys, collection of values, |
|
42 |
* or set of key-value mappings. The <i>order</i> of a map is defined as |
|
43 |
* the order in which the iterators on the map's collection views return their |
|
44 |
* elements. Some map implementations, like the <tt>TreeMap</tt> class, make |
|
45 |
* specific guarantees as to their order; others, like the <tt>HashMap</tt> |
|
46 |
* class, do not. |
|
47 |
* |
|
48 |
* <p>Note: great care must be exercised if mutable objects are used as map |
|
49 |
* keys. The behavior of a map is not specified if the value of an object is |
|
50 |
* changed in a manner that affects <tt>equals</tt> comparisons while the |
|
51 |
* object is a key in the map. A special case of this prohibition is that it |
|
52 |
* is not permissible for a map to contain itself as a key. While it is |
|
53 |
* permissible for a map to contain itself as a value, extreme caution is |
|
54 |
* advised: the <tt>equals</tt> and <tt>hashCode</tt> methods are no longer |
|
55 |
* well defined on such a map. |
|
56 |
* |
|
57 |
* <p>All general-purpose map implementation classes should provide two |
|
58 |
* "standard" constructors: a void (no arguments) constructor which creates an |
|
59 |
* empty map, and a constructor with a single argument of type <tt>Map</tt>, |
|
60 |
* which creates a new map with the same key-value mappings as its argument. |
|
61 |
* In effect, the latter constructor allows the user to copy any map, |
|
62 |
* producing an equivalent map of the desired class. There is no way to |
|
63 |
* enforce this recommendation (as interfaces cannot contain constructors) but |
|
64 |
* all of the general-purpose map implementations in the JDK comply. |
|
65 |
* |
|
66 |
* <p>The "destructive" methods contained in this interface, that is, the |
|
67 |
* methods that modify the map on which they operate, are specified to throw |
|
68 |
* <tt>UnsupportedOperationException</tt> if this map does not support the |
|
69 |
* operation. If this is the case, these methods may, but are not required |
|
70 |
* to, throw an <tt>UnsupportedOperationException</tt> if the invocation would |
|
71 |
* have no effect on the map. For example, invoking the {@link #putAll(Map)} |
|
72 |
* method on an unmodifiable map may, but is not required to, throw the |
|
73 |
* exception if the map whose mappings are to be "superimposed" is empty. |
|
74 |
* |
|
75 |
* <p>Some map implementations have restrictions on the keys and values they |
|
76 |
* may contain. For example, some implementations prohibit null keys and |
|
77 |
* values, and some have restrictions on the types of their keys. Attempting |
|
78 |
* to insert an ineligible key or value throws an unchecked exception, |
|
79 |
* typically <tt>NullPointerException</tt> or <tt>ClassCastException</tt>. |
|
80 |
* Attempting to query the presence of an ineligible key or value may throw an |
|
81 |
* exception, or it may simply return false; some implementations will exhibit |
|
82 |
* the former behavior and some will exhibit the latter. More generally, |
|
83 |
* attempting an operation on an ineligible key or value whose completion |
|
84 |
* would not result in the insertion of an ineligible element into the map may |
|
85 |
* throw an exception or it may succeed, at the option of the implementation. |
|
86 |
* Such exceptions are marked as "optional" in the specification for this |
|
87 |
* interface. |
|
88 |
* |
|
89 |
* <p>Many methods in Collections Framework interfaces are defined |
|
90 |
* in terms of the {@link Object#equals(Object) equals} method. For |
|
91 |
* example, the specification for the {@link #containsKey(Object) |
|
92 |
* containsKey(Object key)} method says: "returns <tt>true</tt> if and |
|
93 |
* only if this map contains a mapping for a key <tt>k</tt> such that |
|
94 |
* <tt>(key==null ? k==null : key.equals(k))</tt>." This specification should |
|
95 |
* <i>not</i> be construed to imply that invoking <tt>Map.containsKey</tt> |
|
96 |
* with a non-null argument <tt>key</tt> will cause <tt>key.equals(k)</tt> to |
|
97 |
* be invoked for any key <tt>k</tt>. Implementations are free to |
|
98 |
* implement optimizations whereby the <tt>equals</tt> invocation is avoided, |
|
99 |
* for example, by first comparing the hash codes of the two keys. (The |
|
100 |
* {@link Object#hashCode()} specification guarantees that two objects with |
|
101 |
* unequal hash codes cannot be equal.) More generally, implementations of |
|
102 |
* the various Collections Framework interfaces are free to take advantage of |
|
103 |
* the specified behavior of underlying {@link Object} methods wherever the |
|
104 |
* implementor deems it appropriate. |
|
105 |
* |
|
20491
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
106 |
* <p>Some map operations which perform recursive traversal of the map may fail |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
107 |
* with an exception for self-referential instances where the map directly or |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
108 |
* indirectly contains itself. This includes the {@code clone()}, |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
109 |
* {@code equals()}, {@code hashCode()} and {@code toString()} methods. |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
110 |
* Implementations may optionally handle the self-referential scenario, however |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
111 |
* most current implementations do not do so. |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
112 |
* |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
113 |
* <p>This interface is a member of the |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
114 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
115 |
* Java Collections Framework</a>. |
eb2dfc7436af
7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents:
20189
diff
changeset
|
116 |
* |
2 | 117 |
* @param <K> the type of keys maintained by this map |
118 |
* @param <V> the type of mapped values |
|
119 |
* |
|
120 |
* @author Josh Bloch |
|
121 |
* @see HashMap |
|
122 |
* @see TreeMap |
|
123 |
* @see Hashtable |
|
124 |
* @see SortedMap |
|
125 |
* @see Collection |
|
126 |
* @see Set |
|
127 |
* @since 1.2 |
|
128 |
*/ |
|
129 |
public interface Map<K,V> { |
|
130 |
// Query Operations |
|
131 |
||
132 |
/** |
|
133 |
* Returns the number of key-value mappings in this map. If the |
|
134 |
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
|
135 |
* <tt>Integer.MAX_VALUE</tt>. |
|
136 |
* |
|
137 |
* @return the number of key-value mappings in this map |
|
138 |
*/ |
|
139 |
int size(); |
|
140 |
||
141 |
/** |
|
142 |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
|
143 |
* |
|
144 |
* @return <tt>true</tt> if this map contains no key-value mappings |
|
145 |
*/ |
|
146 |
boolean isEmpty(); |
|
147 |
||
148 |
/** |
|
149 |
* Returns <tt>true</tt> if this map contains a mapping for the specified |
|
150 |
* key. More formally, returns <tt>true</tt> if and only if |
|
151 |
* this map contains a mapping for a key <tt>k</tt> such that |
|
152 |
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be |
|
153 |
* at most one such mapping.) |
|
154 |
* |
|
155 |
* @param key key whose presence in this map is to be tested |
|
156 |
* @return <tt>true</tt> if this map contains a mapping for the specified |
|
157 |
* key |
|
158 |
* @throws ClassCastException if the key is of an inappropriate type for |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
159 |
* this map |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
160 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 161 |
* @throws NullPointerException if the specified key is null and this map |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
162 |
* does not permit null keys |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
163 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 164 |
*/ |
165 |
boolean containsKey(Object key); |
|
166 |
||
167 |
/** |
|
168 |
* Returns <tt>true</tt> if this map maps one or more keys to the |
|
169 |
* specified value. More formally, returns <tt>true</tt> if and only if |
|
170 |
* this map contains at least one mapping to a value <tt>v</tt> such that |
|
171 |
* <tt>(value==null ? v==null : value.equals(v))</tt>. This operation |
|
172 |
* will probably require time linear in the map size for most |
|
173 |
* implementations of the <tt>Map</tt> interface. |
|
174 |
* |
|
175 |
* @param value value whose presence in this map is to be tested |
|
176 |
* @return <tt>true</tt> if this map maps one or more keys to the |
|
177 |
* specified value |
|
178 |
* @throws ClassCastException if the value is of an inappropriate type for |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
179 |
* this map |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
180 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 181 |
* @throws NullPointerException if the specified value is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
182 |
* map does not permit null values |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
183 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 184 |
*/ |
185 |
boolean containsValue(Object value); |
|
186 |
||
187 |
/** |
|
188 |
* Returns the value to which the specified key is mapped, |
|
189 |
* or {@code null} if this map contains no mapping for the key. |
|
190 |
* |
|
191 |
* <p>More formally, if this map contains a mapping from a key |
|
192 |
* {@code k} to a value {@code v} such that {@code (key==null ? k==null : |
|
193 |
* key.equals(k))}, then this method returns {@code v}; otherwise |
|
194 |
* it returns {@code null}. (There can be at most one such mapping.) |
|
195 |
* |
|
196 |
* <p>If this map permits null values, then a return value of |
|
197 |
* {@code null} does not <i>necessarily</i> indicate that the map |
|
198 |
* contains no mapping for the key; it's also possible that the map |
|
199 |
* explicitly maps the key to {@code null}. The {@link #containsKey |
|
200 |
* containsKey} operation may be used to distinguish these two cases. |
|
201 |
* |
|
202 |
* @param key the key whose associated value is to be returned |
|
203 |
* @return the value to which the specified key is mapped, or |
|
204 |
* {@code null} if this map contains no mapping for the key |
|
205 |
* @throws ClassCastException if the key is of an inappropriate type for |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
206 |
* this map |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
207 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 208 |
* @throws NullPointerException if the specified key is null and this map |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
209 |
* does not permit null keys |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
210 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 211 |
*/ |
212 |
V get(Object key); |
|
213 |
||
214 |
// Modification Operations |
|
215 |
||
216 |
/** |
|
217 |
* Associates the specified value with the specified key in this map |
|
218 |
* (optional operation). If the map previously contained a mapping for |
|
219 |
* the key, the old value is replaced by the specified value. (A map |
|
220 |
* <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only |
|
221 |
* if {@link #containsKey(Object) m.containsKey(k)} would return |
|
222 |
* <tt>true</tt>.) |
|
223 |
* |
|
224 |
* @param key key with which the specified value is to be associated |
|
225 |
* @param value value to be associated with the specified key |
|
226 |
* @return the previous value associated with <tt>key</tt>, or |
|
227 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
|
228 |
* (A <tt>null</tt> return can also indicate that the map |
|
229 |
* previously associated <tt>null</tt> with <tt>key</tt>, |
|
230 |
* if the implementation supports <tt>null</tt> values.) |
|
231 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
|
232 |
* is not supported by this map |
|
233 |
* @throws ClassCastException if the class of the specified key or value |
|
234 |
* prevents it from being stored in this map |
|
235 |
* @throws NullPointerException if the specified key or value is null |
|
236 |
* and this map does not permit null keys or values |
|
237 |
* @throws IllegalArgumentException if some property of the specified key |
|
238 |
* or value prevents it from being stored in this map |
|
239 |
*/ |
|
240 |
V put(K key, V value); |
|
241 |
||
242 |
/** |
|
243 |
* Removes the mapping for a key from this map if it is present |
|
244 |
* (optional operation). More formally, if this map contains a mapping |
|
245 |
* from key <tt>k</tt> to value <tt>v</tt> such that |
|
246 |
* <code>(key==null ? k==null : key.equals(k))</code>, that mapping |
|
247 |
* is removed. (The map can contain at most one such mapping.) |
|
248 |
* |
|
249 |
* <p>Returns the value to which this map previously associated the key, |
|
250 |
* or <tt>null</tt> if the map contained no mapping for the key. |
|
251 |
* |
|
252 |
* <p>If this map permits null values, then a return value of |
|
253 |
* <tt>null</tt> does not <i>necessarily</i> indicate that the map |
|
254 |
* contained no mapping for the key; it's also possible that the map |
|
255 |
* explicitly mapped the key to <tt>null</tt>. |
|
256 |
* |
|
257 |
* <p>The map will not contain a mapping for the specified key once the |
|
258 |
* call returns. |
|
259 |
* |
|
260 |
* @param key key whose mapping is to be removed from the map |
|
261 |
* @return the previous value associated with <tt>key</tt>, or |
|
262 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
|
263 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
|
264 |
* is not supported by this map |
|
265 |
* @throws ClassCastException if the key is of an inappropriate type for |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
266 |
* this map |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
267 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 268 |
* @throws NullPointerException if the specified key is null and this |
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
269 |
* map does not permit null keys |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
270 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 271 |
*/ |
272 |
V remove(Object key); |
|
273 |
||
274 |
||
275 |
// Bulk Operations |
|
276 |
||
277 |
/** |
|
278 |
* Copies all of the mappings from the specified map to this map |
|
279 |
* (optional operation). The effect of this call is equivalent to that |
|
280 |
* of calling {@link #put(Object,Object) put(k, v)} on this map once |
|
281 |
* for each mapping from key <tt>k</tt> to value <tt>v</tt> in the |
|
282 |
* specified map. The behavior of this operation is undefined if the |
|
283 |
* specified map is modified while the operation is in progress. |
|
284 |
* |
|
285 |
* @param m mappings to be stored in this map |
|
286 |
* @throws UnsupportedOperationException if the <tt>putAll</tt> operation |
|
287 |
* is not supported by this map |
|
288 |
* @throws ClassCastException if the class of a key or value in the |
|
289 |
* specified map prevents it from being stored in this map |
|
290 |
* @throws NullPointerException if the specified map is null, or if |
|
291 |
* this map does not permit null keys or values, and the |
|
292 |
* specified map contains null keys or values |
|
293 |
* @throws IllegalArgumentException if some property of a key or value in |
|
294 |
* the specified map prevents it from being stored in this map |
|
295 |
*/ |
|
296 |
void putAll(Map<? extends K, ? extends V> m); |
|
297 |
||
298 |
/** |
|
299 |
* Removes all of the mappings from this map (optional operation). |
|
300 |
* The map will be empty after this call returns. |
|
301 |
* |
|
302 |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
|
303 |
* is not supported by this map |
|
304 |
*/ |
|
305 |
void clear(); |
|
306 |
||
307 |
||
308 |
// Views |
|
309 |
||
310 |
/** |
|
311 |
* Returns a {@link Set} view of the keys contained in this map. |
|
312 |
* The set is backed by the map, so changes to the map are |
|
313 |
* reflected in the set, and vice-versa. If the map is modified |
|
314 |
* while an iteration over the set is in progress (except through |
|
315 |
* the iterator's own <tt>remove</tt> operation), the results of |
|
316 |
* the iteration are undefined. The set supports element removal, |
|
317 |
* which removes the corresponding mapping from the map, via the |
|
318 |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
|
319 |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
|
320 |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
|
321 |
* operations. |
|
322 |
* |
|
323 |
* @return a set view of the keys contained in this map |
|
324 |
*/ |
|
325 |
Set<K> keySet(); |
|
326 |
||
327 |
/** |
|
328 |
* Returns a {@link Collection} view of the values contained in this map. |
|
329 |
* The collection is backed by the map, so changes to the map are |
|
330 |
* reflected in the collection, and vice-versa. If the map is |
|
331 |
* modified while an iteration over the collection is in progress |
|
332 |
* (except through the iterator's own <tt>remove</tt> operation), |
|
333 |
* the results of the iteration are undefined. The collection |
|
334 |
* supports element removal, which removes the corresponding |
|
335 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
336 |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
|
337 |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
|
338 |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
|
339 |
* |
|
340 |
* @return a collection view of the values contained in this map |
|
341 |
*/ |
|
342 |
Collection<V> values(); |
|
343 |
||
344 |
/** |
|
345 |
* Returns a {@link Set} view of the mappings contained in this map. |
|
346 |
* The set is backed by the map, so changes to the map are |
|
347 |
* reflected in the set, and vice-versa. If the map is modified |
|
348 |
* while an iteration over the set is in progress (except through |
|
349 |
* the iterator's own <tt>remove</tt> operation, or through the |
|
350 |
* <tt>setValue</tt> operation on a map entry returned by the |
|
351 |
* iterator) the results of the iteration are undefined. The set |
|
352 |
* supports element removal, which removes the corresponding |
|
353 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
354 |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and |
|
355 |
* <tt>clear</tt> operations. It does not support the |
|
356 |
* <tt>add</tt> or <tt>addAll</tt> operations. |
|
357 |
* |
|
358 |
* @return a set view of the mappings contained in this map |
|
359 |
*/ |
|
360 |
Set<Map.Entry<K, V>> entrySet(); |
|
361 |
||
362 |
/** |
|
363 |
* A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns |
|
364 |
* a collection-view of the map, whose elements are of this class. The |
|
365 |
* <i>only</i> way to obtain a reference to a map entry is from the |
|
366 |
* iterator of this collection-view. These <tt>Map.Entry</tt> objects are |
|
367 |
* valid <i>only</i> for the duration of the iteration; more formally, |
|
368 |
* the behavior of a map entry is undefined if the backing map has been |
|
369 |
* modified after the entry was returned by the iterator, except through |
|
370 |
* the <tt>setValue</tt> operation on the map entry. |
|
371 |
* |
|
372 |
* @see Map#entrySet() |
|
373 |
* @since 1.2 |
|
374 |
*/ |
|
375 |
interface Entry<K,V> { |
|
376 |
/** |
|
377 |
* Returns the key corresponding to this entry. |
|
378 |
* |
|
379 |
* @return the key corresponding to this entry |
|
380 |
* @throws IllegalStateException implementations may, but are not |
|
381 |
* required to, throw this exception if the entry has been |
|
382 |
* removed from the backing map. |
|
383 |
*/ |
|
384 |
K getKey(); |
|
385 |
||
386 |
/** |
|
387 |
* Returns the value corresponding to this entry. If the mapping |
|
388 |
* has been removed from the backing map (by the iterator's |
|
389 |
* <tt>remove</tt> operation), the results of this call are undefined. |
|
390 |
* |
|
391 |
* @return the value corresponding to this entry |
|
392 |
* @throws IllegalStateException implementations may, but are not |
|
393 |
* required to, throw this exception if the entry has been |
|
394 |
* removed from the backing map. |
|
395 |
*/ |
|
396 |
V getValue(); |
|
397 |
||
398 |
/** |
|
399 |
* Replaces the value corresponding to this entry with the specified |
|
400 |
* value (optional operation). (Writes through to the map.) The |
|
401 |
* behavior of this call is undefined if the mapping has already been |
|
402 |
* removed from the map (by the iterator's <tt>remove</tt> operation). |
|
403 |
* |
|
404 |
* @param value new value to be stored in this entry |
|
405 |
* @return old value corresponding to the entry |
|
406 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
|
407 |
* is not supported by the backing map |
|
408 |
* @throws ClassCastException if the class of the specified value |
|
409 |
* prevents it from being stored in the backing map |
|
410 |
* @throws NullPointerException if the backing map does not permit |
|
411 |
* null values, and the specified value is null |
|
412 |
* @throws IllegalArgumentException if some property of this value |
|
413 |
* prevents it from being stored in the backing map |
|
414 |
* @throws IllegalStateException implementations may, but are not |
|
415 |
* required to, throw this exception if the entry has been |
|
416 |
* removed from the backing map. |
|
417 |
*/ |
|
418 |
V setValue(V value); |
|
419 |
||
420 |
/** |
|
421 |
* Compares the specified object with this entry for equality. |
|
422 |
* Returns <tt>true</tt> if the given object is also a map entry and |
|
423 |
* the two entries represent the same mapping. More formally, two |
|
424 |
* entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping |
|
425 |
* if<pre> |
|
426 |
* (e1.getKey()==null ? |
|
427 |
* e2.getKey()==null : e1.getKey().equals(e2.getKey())) && |
|
428 |
* (e1.getValue()==null ? |
|
429 |
* e2.getValue()==null : e1.getValue().equals(e2.getValue())) |
|
430 |
* </pre> |
|
431 |
* This ensures that the <tt>equals</tt> method works properly across |
|
432 |
* different implementations of the <tt>Map.Entry</tt> interface. |
|
433 |
* |
|
434 |
* @param o object to be compared for equality with this map entry |
|
435 |
* @return <tt>true</tt> if the specified object is equal to this map |
|
436 |
* entry |
|
437 |
*/ |
|
438 |
boolean equals(Object o); |
|
439 |
||
440 |
/** |
|
441 |
* Returns the hash code value for this map entry. The hash code |
|
442 |
* of a map entry <tt>e</tt> is defined to be: <pre> |
|
443 |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
|
444 |
* (e.getValue()==null ? 0 : e.getValue().hashCode()) |
|
445 |
* </pre> |
|
446 |
* This ensures that <tt>e1.equals(e2)</tt> implies that |
|
447 |
* <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries |
|
448 |
* <tt>e1</tt> and <tt>e2</tt>, as required by the general |
|
449 |
* contract of <tt>Object.hashCode</tt>. |
|
450 |
* |
|
451 |
* @return the hash code value for this map entry |
|
452 |
* @see Object#hashCode() |
|
453 |
* @see Object#equals(Object) |
|
454 |
* @see #equals(Object) |
|
455 |
*/ |
|
456 |
int hashCode(); |
|
18571 | 457 |
|
458 |
/** |
|
459 |
* Returns a comparator that compares {@link Map.Entry} in natural order on key. |
|
460 |
* |
|
461 |
* <p>The returned comparator is serializable and throws {@link |
|
462 |
* NullPointerException} when comparing an entry with a null key. |
|
463 |
* |
|
464 |
* @param <K> the {@link Comparable} type of then map keys |
|
465 |
* @param <V> the type of the map values |
|
466 |
* @return a comparator that compares {@link Map.Entry} in natural order on key. |
|
467 |
* @see Comparable |
|
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
468 |
* @since 1.8 |
18571 | 469 |
*/ |
470 |
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() { |
|
471 |
return (Comparator<Map.Entry<K, V>> & Serializable) |
|
472 |
(c1, c2) -> c1.getKey().compareTo(c2.getKey()); |
|
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* Returns a comparator that compares {@link Map.Entry} in natural order on value. |
|
477 |
* |
|
478 |
* <p>The returned comparator is serializable and throws {@link |
|
479 |
* NullPointerException} when comparing an entry with null values. |
|
480 |
* |
|
481 |
* @param <K> the type of the map keys |
|
482 |
* @param <V> the {@link Comparable} type of the map values |
|
483 |
* @return a comparator that compares {@link Map.Entry} in natural order on value. |
|
484 |
* @see Comparable |
|
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
485 |
* @since 1.8 |
18571 | 486 |
*/ |
487 |
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() { |
|
488 |
return (Comparator<Map.Entry<K, V>> & Serializable) |
|
489 |
(c1, c2) -> c1.getValue().compareTo(c2.getValue()); |
|
490 |
} |
|
491 |
||
492 |
/** |
|
493 |
* Returns a comparator that compares {@link Map.Entry} by key using the given |
|
494 |
* {@link Comparator}. |
|
495 |
* |
|
496 |
* <p>The returned comparator is serializable if the specified comparator |
|
497 |
* is also serializable. |
|
498 |
* |
|
499 |
* @param <K> the type of the map keys |
|
500 |
* @param <V> the type of the map values |
|
501 |
* @param cmp the key {@link Comparator} |
|
502 |
* @return a comparator that compares {@link Map.Entry} by the key. |
|
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
503 |
* @since 1.8 |
18571 | 504 |
*/ |
505 |
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) { |
|
506 |
Objects.requireNonNull(cmp); |
|
507 |
return (Comparator<Map.Entry<K, V>> & Serializable) |
|
508 |
(c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Returns a comparator that compares {@link Map.Entry} by value using the given |
|
513 |
* {@link Comparator}. |
|
514 |
* |
|
515 |
* <p>The returned comparator is serializable if the specified comparator |
|
516 |
* is also serializable. |
|
517 |
* |
|
518 |
* @param <K> the type of the map keys |
|
519 |
* @param <V> the type of the map values |
|
520 |
* @param cmp the value {@link Comparator} |
|
521 |
* @return a comparator that compares {@link Map.Entry} by the value. |
|
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
522 |
* @since 1.8 |
18571 | 523 |
*/ |
524 |
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) { |
|
525 |
Objects.requireNonNull(cmp); |
|
526 |
return (Comparator<Map.Entry<K, V>> & Serializable) |
|
527 |
(c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); |
|
528 |
} |
|
2 | 529 |
} |
530 |
||
531 |
// Comparison and hashing |
|
532 |
||
533 |
/** |
|
534 |
* Compares the specified object with this map for equality. Returns |
|
535 |
* <tt>true</tt> if the given object is also a map and the two maps |
|
536 |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
|
537 |
* <tt>m2</tt> represent the same mappings if |
|
538 |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
|
539 |
* <tt>equals</tt> method works properly across different implementations |
|
540 |
* of the <tt>Map</tt> interface. |
|
541 |
* |
|
542 |
* @param o object to be compared for equality with this map |
|
543 |
* @return <tt>true</tt> if the specified object is equal to this map |
|
544 |
*/ |
|
545 |
boolean equals(Object o); |
|
546 |
||
547 |
/** |
|
548 |
* Returns the hash code value for this map. The hash code of a map is |
|
549 |
* defined to be the sum of the hash codes of each entry in the map's |
|
550 |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
|
551 |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
|
552 |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
|
553 |
* {@link Object#hashCode}. |
|
554 |
* |
|
555 |
* @return the hash code value for this map |
|
556 |
* @see Map.Entry#hashCode() |
|
557 |
* @see Object#equals(Object) |
|
558 |
* @see #equals(Object) |
|
559 |
*/ |
|
560 |
int hashCode(); |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
5506
diff
changeset
|
561 |
|
16867 | 562 |
// Defaultable methods |
563 |
||
564 |
/** |
|
21339 | 565 |
* Returns the value to which the specified key is mapped, or |
566 |
* {@code defaultValue} if this map contains no mapping for the key. |
|
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
567 |
* |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
568 |
* @implSpec |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
569 |
* The default implementation makes no guarantees about synchronization |
20868
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
570 |
* or atomicity properties of this method. Any implementation providing |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
571 |
* atomicity guarantees must override this method and document its |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
572 |
* concurrency properties. |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
573 |
* |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
574 |
* @param key the key whose associated value is to be returned |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
575 |
* @param defaultValue the default mapping of the key |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
576 |
* @return the value to which the specified key is mapped, or |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
577 |
* {@code defaultValue} if this map contains no mapping for the key |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
578 |
* @throws ClassCastException if the key is of an inappropriate type for |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
579 |
* this map |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
580 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
581 |
* @throws NullPointerException if the specified key is null and this map |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
582 |
* does not permit null keys |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
583 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
584 |
* @since 1.8 |
dcad3ccb889a
8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents:
20491
diff
changeset
|
585 |
*/ |
16867 | 586 |
default V getOrDefault(Object key, V defaultValue) { |
587 |
V v; |
|
588 |
return (((v = get(key)) != null) || containsKey(key)) |
|
589 |
? v |
|
590 |
: defaultValue; |
|
591 |
} |
|
592 |
||
593 |
/** |
|
21339 | 594 |
* Performs the given action for each entry in this map until all entries |
595 |
* have been processed or the action throws an exception. Unless |
|
596 |
* otherwise specified by the implementing class, actions are performed in |
|
597 |
* the order of entry set iteration (if an iteration order is specified.) |
|
16867 | 598 |
* Exceptions thrown by the action are relayed to the caller. |
599 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
600 |
* @implSpec |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
601 |
* The default implementation is equivalent to, for this {@code map}: |
16867 | 602 |
* <pre> {@code |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
603 |
* for (Map.Entry<K, V> entry : map.entrySet()) |
16867 | 604 |
* action.accept(entry.getKey(), entry.getValue()); |
605 |
* }</pre> |
|
606 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
607 |
* The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
608 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
609 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
610 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
611 |
* |
16867 | 612 |
* @param action The action to be performed for each entry |
613 |
* @throws NullPointerException if the specified action is null |
|
614 |
* @throws ConcurrentModificationException if an entry is found to be |
|
615 |
* removed during iteration |
|
616 |
* @since 1.8 |
|
617 |
*/ |
|
618 |
default void forEach(BiConsumer<? super K, ? super V> action) { |
|
619 |
Objects.requireNonNull(action); |
|
620 |
for (Map.Entry<K, V> entry : entrySet()) { |
|
621 |
K k; |
|
622 |
V v; |
|
623 |
try { |
|
624 |
k = entry.getKey(); |
|
625 |
v = entry.getValue(); |
|
626 |
} catch(IllegalStateException ise) { |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
627 |
// this usually means the entry is no longer in the map. |
16867 | 628 |
throw new ConcurrentModificationException(ise); |
629 |
} |
|
630 |
action.accept(k, v); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
/** |
|
635 |
* Replaces each entry's value with the result of invoking the given |
|
21339 | 636 |
* function on that entry until all entries have been processed or the |
637 |
* function throws an exception. Exceptions thrown by the function are |
|
638 |
* relayed to the caller. |
|
16867 | 639 |
* |
640 |
* @implSpec |
|
641 |
* <p>The default implementation is equivalent to, for this {@code map}: |
|
642 |
* <pre> {@code |
|
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
643 |
* for (Map.Entry<K, V> entry : map.entrySet()) |
16867 | 644 |
* entry.setValue(function.apply(entry.getKey(), entry.getValue())); |
645 |
* }</pre> |
|
646 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
647 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
648 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
649 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
650 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
651 |
* |
16867 | 652 |
* @param function the function to apply to each entry |
653 |
* @throws UnsupportedOperationException if the {@code set} operation |
|
654 |
* is not supported by this map's entry set iterator. |
|
655 |
* @throws ClassCastException if the class of a replacement value |
|
656 |
* prevents it from being stored in this map |
|
657 |
* @throws NullPointerException if the specified function is null, or the |
|
658 |
* specified replacement value is null, and this map does not permit null |
|
659 |
* values |
|
660 |
* @throws ClassCastException if a replacement value is of an inappropriate |
|
661 |
* type for this map |
|
662 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
663 |
* @throws NullPointerException if function or a replacement value is null, |
|
664 |
* and this map does not permit null keys or values |
|
665 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
666 |
* @throws IllegalArgumentException if some property of a replacement value |
|
667 |
* prevents it from being stored in this map |
|
668 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
669 |
* @throws ConcurrentModificationException if an entry is found to be |
|
670 |
* removed during iteration |
|
671 |
* @since 1.8 |
|
672 |
*/ |
|
673 |
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
674 |
Objects.requireNonNull(function); |
|
675 |
for (Map.Entry<K, V> entry : entrySet()) { |
|
676 |
K k; |
|
677 |
V v; |
|
678 |
try { |
|
679 |
k = entry.getKey(); |
|
680 |
v = entry.getValue(); |
|
681 |
} catch(IllegalStateException ise) { |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
682 |
// this usually means the entry is no longer in the map. |
16867 | 683 |
throw new ConcurrentModificationException(ise); |
684 |
} |
|
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
685 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
686 |
// ise thrown from function is not a cme. |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
687 |
v = function.apply(k, v); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
688 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
689 |
try { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
690 |
entry.setValue(v); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
691 |
} catch(IllegalStateException ise) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
692 |
// this usually means the entry is no longer in the map. |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
693 |
throw new ConcurrentModificationException(ise); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
16867
diff
changeset
|
694 |
} |
16867 | 695 |
} |
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* If the specified key is not already associated with a value (or is mapped |
|
700 |
* to {@code null}) associates it with the given value and returns |
|
701 |
* {@code null}, else returns the current value. |
|
702 |
* |
|
703 |
* @implSpec |
|
704 |
* The default implementation is equivalent to, for this {@code |
|
705 |
* map}: |
|
706 |
* |
|
707 |
* <pre> {@code |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
708 |
* V v = map.get(key); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
709 |
* if (v == null) |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
710 |
* v = map.put(key, value); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
711 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
712 |
* return v; |
16867 | 713 |
* }</pre> |
714 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
715 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
716 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
717 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
718 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
719 |
* |
16867 | 720 |
* @param key key with which the specified value is to be associated |
721 |
* @param value value to be associated with the specified key |
|
722 |
* @return the previous value associated with the specified key, or |
|
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
723 |
* {@code null} if there was no mapping for the key. |
16867 | 724 |
* (A {@code null} return can also indicate that the map |
725 |
* previously associated {@code null} with the key, |
|
726 |
* if the implementation supports null values.) |
|
727 |
* @throws UnsupportedOperationException if the {@code put} operation |
|
728 |
* is not supported by this map |
|
729 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
730 |
* @throws ClassCastException if the key or value is of an inappropriate |
|
731 |
* type for this map |
|
732 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
733 |
* @throws NullPointerException if the specified key or value is null, |
|
734 |
* and this map does not permit null keys or values |
|
735 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
736 |
* @throws IllegalArgumentException if some property of the specified key |
|
737 |
* or value prevents it from being stored in this map |
|
738 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
739 |
* @since 1.8 |
|
740 |
*/ |
|
741 |
default V putIfAbsent(K key, V value) { |
|
742 |
V v = get(key); |
|
743 |
if (v == null) { |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
744 |
v = put(key, value); |
16867 | 745 |
} |
746 |
||
747 |
return v; |
|
748 |
} |
|
749 |
||
750 |
/** |
|
751 |
* Removes the entry for the specified key only if it is currently |
|
752 |
* mapped to the specified value. |
|
753 |
* |
|
754 |
* @implSpec |
|
755 |
* The default implementation is equivalent to, for this {@code map}: |
|
756 |
* |
|
757 |
* <pre> {@code |
|
758 |
* if (map.containsKey(key) && Objects.equals(map.get(key), value)) { |
|
759 |
* map.remove(key); |
|
760 |
* return true; |
|
761 |
* } else |
|
762 |
* return false; |
|
763 |
* }</pre> |
|
764 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
765 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
766 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
767 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
768 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
769 |
* |
16867 | 770 |
* @param key key with which the specified value is associated |
771 |
* @param value value expected to be associated with the specified key |
|
772 |
* @return {@code true} if the value was removed |
|
773 |
* @throws UnsupportedOperationException if the {@code remove} operation |
|
774 |
* is not supported by this map |
|
775 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
776 |
* @throws ClassCastException if the key or value is of an inappropriate |
|
777 |
* type for this map |
|
778 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
779 |
* @throws NullPointerException if the specified key or value is null, |
|
780 |
* and this map does not permit null keys or values |
|
781 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
782 |
* @since 1.8 |
|
783 |
*/ |
|
784 |
default boolean remove(Object key, Object value) { |
|
785 |
Object curValue = get(key); |
|
786 |
if (!Objects.equals(curValue, value) || |
|
787 |
(curValue == null && !containsKey(key))) { |
|
788 |
return false; |
|
789 |
} |
|
790 |
remove(key); |
|
791 |
return true; |
|
792 |
} |
|
793 |
||
794 |
/** |
|
795 |
* Replaces the entry for the specified key only if currently |
|
796 |
* mapped to the specified value. |
|
797 |
* |
|
798 |
* @implSpec |
|
799 |
* The default implementation is equivalent to, for this {@code map}: |
|
800 |
* |
|
801 |
* <pre> {@code |
|
802 |
* if (map.containsKey(key) && Objects.equals(map.get(key), value)) { |
|
803 |
* map.put(key, newValue); |
|
804 |
* return true; |
|
805 |
* } else |
|
806 |
* return false; |
|
807 |
* }</pre> |
|
808 |
* |
|
19855 | 809 |
* The default implementation does not throw NullPointerException |
810 |
* for maps that do not support null values if oldValue is null unless |
|
811 |
* newValue is also null. |
|
812 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
813 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
814 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
815 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
816 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
817 |
* |
16867 | 818 |
* @param key key with which the specified value is associated |
819 |
* @param oldValue value expected to be associated with the specified key |
|
820 |
* @param newValue value to be associated with the specified key |
|
821 |
* @return {@code true} if the value was replaced |
|
822 |
* @throws UnsupportedOperationException if the {@code put} operation |
|
823 |
* is not supported by this map |
|
824 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
825 |
* @throws ClassCastException if the class of a specified key or value |
|
826 |
* prevents it from being stored in this map |
|
19855 | 827 |
* @throws NullPointerException if a specified key or newValue is null, |
16867 | 828 |
* and this map does not permit null keys or values |
19855 | 829 |
* @throws NullPointerException if oldValue is null and this map does not |
830 |
* permit null values |
|
831 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
16867 | 832 |
* @throws IllegalArgumentException if some property of a specified key |
833 |
* or value prevents it from being stored in this map |
|
834 |
* @since 1.8 |
|
835 |
*/ |
|
836 |
default boolean replace(K key, V oldValue, V newValue) { |
|
837 |
Object curValue = get(key); |
|
838 |
if (!Objects.equals(curValue, oldValue) || |
|
839 |
(curValue == null && !containsKey(key))) { |
|
840 |
return false; |
|
841 |
} |
|
842 |
put(key, newValue); |
|
843 |
return true; |
|
844 |
} |
|
845 |
||
846 |
/** |
|
847 |
* Replaces the entry for the specified key only if it is |
|
848 |
* currently mapped to some value. |
|
849 |
* |
|
850 |
* @implSpec |
|
851 |
* The default implementation is equivalent to, for this {@code map}: |
|
852 |
* |
|
853 |
* <pre> {@code |
|
854 |
* if (map.containsKey(key)) { |
|
855 |
* return map.put(key, value); |
|
856 |
* } else |
|
857 |
* return null; |
|
858 |
* }</pre> |
|
859 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
860 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
861 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
862 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
863 |
* concurrency properties. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
864 |
* |
16867 | 865 |
* @param key key with which the specified value is associated |
866 |
* @param value value to be associated with the specified key |
|
867 |
* @return the previous value associated with the specified key, or |
|
868 |
* {@code null} if there was no mapping for the key. |
|
869 |
* (A {@code null} return can also indicate that the map |
|
870 |
* previously associated {@code null} with the key, |
|
871 |
* if the implementation supports null values.) |
|
872 |
* @throws UnsupportedOperationException if the {@code put} operation |
|
873 |
* is not supported by this map |
|
874 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
875 |
* @throws ClassCastException if the class of the specified key or value |
|
876 |
* prevents it from being stored in this map |
|
877 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
878 |
* @throws NullPointerException if the specified key or value is null, |
|
879 |
* and this map does not permit null keys or values |
|
880 |
* @throws IllegalArgumentException if some property of the specified key |
|
881 |
* or value prevents it from being stored in this map |
|
882 |
* @since 1.8 |
|
883 |
*/ |
|
884 |
default V replace(K key, V value) { |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
885 |
V curValue; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
886 |
if (((curValue = get(key)) != null) || containsKey(key)) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
887 |
curValue = put(key, value); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
888 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
889 |
return curValue; |
16867 | 890 |
} |
891 |
||
892 |
/** |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
893 |
* If the specified key is not already associated with a value (or is mapped |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
894 |
* to {@code null}), attempts to compute its value using the given mapping |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
895 |
* function and enters it into this map unless {@code null}. |
16867 | 896 |
* |
897 |
* <p>If the function returns {@code null} no mapping is recorded. If |
|
898 |
* the function itself throws an (unchecked) exception, the |
|
899 |
* exception is rethrown, and no mapping is recorded. The most |
|
900 |
* common usage is to construct a new object serving as an initial |
|
901 |
* mapped value or memoized result, as in: |
|
902 |
* |
|
903 |
* <pre> {@code |
|
904 |
* map.computeIfAbsent(key, k -> new Value(f(k))); |
|
905 |
* }</pre> |
|
906 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
907 |
* <p>Or to implement a multi-value map, {@code Map<K,Collection<V>>}, |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
908 |
* supporting multiple values per key: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
909 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
910 |
* <pre> {@code |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
911 |
* map.computeIfAbsent(key, k -> new HashSet<V>()).add(v); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
912 |
* }</pre> |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
913 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
914 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
915 |
* @implSpec |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
916 |
* The default implementation is equivalent to the following steps for this |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
917 |
* {@code map}, then returning the current value or {@code null} if now |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
918 |
* absent: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
919 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
920 |
* <pre> {@code |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
921 |
* if (map.get(key) == null) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
922 |
* V newValue = mappingFunction.apply(key); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
923 |
* if (newValue != null) |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
924 |
* map.put(key, newValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
925 |
* } |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
926 |
* }</pre> |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
927 |
* |
16867 | 928 |
* <p>The default implementation makes no guarantees about synchronization |
929 |
* or atomicity properties of this method. Any implementation providing |
|
930 |
* atomicity guarantees must override this method and document its |
|
931 |
* concurrency properties. In particular, all implementations of |
|
932 |
* subinterface {@link java.util.concurrent.ConcurrentMap} must document |
|
933 |
* whether the function is applied once atomically only if the value is not |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
934 |
* present. |
16867 | 935 |
* |
936 |
* @param key key with which the specified value is to be associated |
|
937 |
* @param mappingFunction the function to compute a value |
|
938 |
* @return the current (existing or computed) value associated with |
|
939 |
* the specified key, or null if the computed value is null |
|
940 |
* @throws NullPointerException if the specified key is null and |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
941 |
* this map does not support null keys, or the mappingFunction |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
942 |
* is null |
16867 | 943 |
* @throws UnsupportedOperationException if the {@code put} operation |
944 |
* is not supported by this map |
|
945 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
946 |
* @throws ClassCastException if the class of the specified key or value |
|
947 |
* prevents it from being stored in this map |
|
948 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
949 |
* @since 1.8 |
|
950 |
*/ |
|
951 |
default V computeIfAbsent(K key, |
|
952 |
Function<? super K, ? extends V> mappingFunction) { |
|
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
953 |
Objects.requireNonNull(mappingFunction); |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
954 |
V v; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
955 |
if ((v = get(key)) == null) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
956 |
V newValue; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
957 |
if ((newValue = mappingFunction.apply(key)) != null) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
958 |
put(key, newValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
959 |
return newValue; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
960 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
961 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
962 |
|
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
963 |
return v; |
16867 | 964 |
} |
965 |
||
966 |
/** |
|
967 |
* If the value for the specified key is present and non-null, attempts to |
|
968 |
* compute a new mapping given the key and its current mapped value. |
|
969 |
* |
|
970 |
* <p>If the function returns {@code null}, the mapping is removed. If the |
|
971 |
* function itself throws an (unchecked) exception, the exception is |
|
972 |
* rethrown, and the current mapping is left unchanged. |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
973 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
974 |
* @implSpec |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
975 |
* The default implementation is equivalent to performing the following |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
976 |
* steps for this {@code map}, then returning the current value or |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
977 |
* {@code null} if now absent: |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
978 |
* |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
979 |
* <pre> {@code |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
980 |
* if (map.get(key) != null) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
981 |
* V oldValue = map.get(key); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
982 |
* V newValue = remappingFunction.apply(key, oldValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
983 |
* if (newValue != null) |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
984 |
* map.put(key, newValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
985 |
* else |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
986 |
* map.remove(key); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
987 |
* } |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
988 |
* }</pre> |
16867 | 989 |
* |
990 |
* <p>The default implementation makes no guarantees about synchronization |
|
991 |
* or atomicity properties of this method. Any implementation providing |
|
992 |
* atomicity guarantees must override this method and document its |
|
993 |
* concurrency properties. In particular, all implementations of |
|
994 |
* subinterface {@link java.util.concurrent.ConcurrentMap} must document |
|
995 |
* whether the function is applied once atomically only if the value is not |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
996 |
* present. |
16867 | 997 |
* |
998 |
* @param key key with which the specified value is to be associated |
|
999 |
* @param remappingFunction the function to compute a value |
|
1000 |
* @return the new value associated with the specified key, or null if none |
|
1001 |
* @throws NullPointerException if the specified key is null and |
|
1002 |
* this map does not support null keys, or the |
|
1003 |
* remappingFunction is null |
|
1004 |
* @throws UnsupportedOperationException if the {@code put} operation |
|
1005 |
* is not supported by this map |
|
1006 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
1007 |
* @throws ClassCastException if the class of the specified key or value |
|
1008 |
* prevents it from being stored in this map |
|
1009 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
1010 |
* @since 1.8 |
|
1011 |
*/ |
|
1012 |
default V computeIfPresent(K key, |
|
1013 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
1014 |
Objects.requireNonNull(remappingFunction); |
16867 | 1015 |
V oldValue; |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1016 |
if ((oldValue = get(key)) != null) { |
16867 | 1017 |
V newValue = remappingFunction.apply(key, oldValue); |
1018 |
if (newValue != null) { |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1019 |
put(key, newValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1020 |
return newValue; |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1021 |
} else { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1022 |
remove(key); |
16867 | 1023 |
return null; |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1024 |
} |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1025 |
} else { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1026 |
return null; |
16867 | 1027 |
} |
1028 |
} |
|
1029 |
||
1030 |
/** |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1031 |
* Attempts to compute a mapping for the specified key and its current |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1032 |
* mapped value (or {@code null} if there is no current mapping). For |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1033 |
* example, to either create or append a {@code String} msg to a value |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1034 |
* mapping: |
16867 | 1035 |
* |
1036 |
* <pre> {@code |
|
1037 |
* map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))}</pre> |
|
1038 |
* (Method {@link #merge merge()} is often simpler to use for such purposes.) |
|
1039 |
* |
|
1040 |
* <p>If the function returns {@code null}, the mapping is removed (or |
|
1041 |
* remains absent if initially absent). If the function itself throws an |
|
1042 |
* (unchecked) exception, the exception is rethrown, and the current mapping |
|
1043 |
* is left unchanged. |
|
1044 |
* |
|
1045 |
* @implSpec |
|
1046 |
* The default implementation is equivalent to performing the following |
|
1047 |
* steps for this {@code map}, then returning the current value or |
|
1048 |
* {@code null} if absent: |
|
1049 |
* |
|
1050 |
* <pre> {@code |
|
1051 |
* V oldValue = map.get(key); |
|
1052 |
* V newValue = remappingFunction.apply(key, oldValue); |
|
1053 |
* if (oldValue != null ) { |
|
1054 |
* if (newValue != null) |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1055 |
* map.put(key, newValue); |
16867 | 1056 |
* else |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1057 |
* map.remove(key); |
16867 | 1058 |
* } else { |
1059 |
* if (newValue != null) |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1060 |
* map.put(key, newValue); |
16867 | 1061 |
* else |
1062 |
* return null; |
|
1063 |
* } |
|
1064 |
* }</pre> |
|
1065 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1066 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1067 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1068 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1069 |
* concurrency properties. In particular, all implementations of |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1070 |
* subinterface {@link java.util.concurrent.ConcurrentMap} must document |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1071 |
* whether the function is applied once atomically only if the value is not |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1072 |
* present. |
16867 | 1073 |
* |
1074 |
* @param key key with which the specified value is to be associated |
|
1075 |
* @param remappingFunction the function to compute a value |
|
1076 |
* @return the new value associated with the specified key, or null if none |
|
1077 |
* @throws NullPointerException if the specified key is null and |
|
1078 |
* this map does not support null keys, or the |
|
1079 |
* remappingFunction is null |
|
1080 |
* @throws UnsupportedOperationException if the {@code put} operation |
|
1081 |
* is not supported by this map |
|
1082 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
1083 |
* @throws ClassCastException if the class of the specified key or value |
|
1084 |
* prevents it from being stored in this map |
|
1085 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
1086 |
* @since 1.8 |
|
1087 |
*/ |
|
1088 |
default V compute(K key, |
|
1089 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
|
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
1090 |
Objects.requireNonNull(remappingFunction); |
16867 | 1091 |
V oldValue = get(key); |
18532
0bbca0914946
8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents:
18280
diff
changeset
|
1092 |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1093 |
V newValue = remappingFunction.apply(key, oldValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1094 |
if (newValue == null) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1095 |
// delete mapping |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1096 |
if (oldValue != null || containsKey(key)) { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1097 |
// something to remove |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1098 |
remove(key); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1099 |
return null; |
16867 | 1100 |
} else { |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1101 |
// nothing to do. Leave things as they were. |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1102 |
return null; |
16867 | 1103 |
} |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1104 |
} else { |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1105 |
// add or replace old mapping |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1106 |
put(key, newValue); |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1107 |
return newValue; |
16867 | 1108 |
} |
1109 |
} |
|
1110 |
||
1111 |
/** |
|
1112 |
* If the specified key is not already associated with a value or is |
|
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1113 |
* associated with null, associates it with the given non-null value. |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1114 |
* Otherwise, replaces the associated value with the results of the given |
16867 | 1115 |
* remapping function, or removes if the result is {@code null}. This |
1116 |
* method may be of use when combining multiple mapped values for a key. |
|
1117 |
* For example, to either create or append a {@code String msg} to a |
|
1118 |
* value mapping: |
|
1119 |
* |
|
1120 |
* <pre> {@code |
|
1121 |
* map.merge(key, msg, String::concat) |
|
1122 |
* }</pre> |
|
1123 |
* |
|
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1124 |
* <p>If the function returns {@code null} the mapping is removed. If the |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1125 |
* function itself throws an (unchecked) exception, the exception is |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1126 |
* rethrown, and the current mapping is left unchanged. |
16867 | 1127 |
* |
1128 |
* @implSpec |
|
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1129 |
* The default implementation is equivalent to performing the following |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1130 |
* steps for this {@code map}, then returning the current value or |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1131 |
* {@code null} if absent: |
16867 | 1132 |
* |
1133 |
* <pre> {@code |
|
1134 |
* V oldValue = map.get(key); |
|
1135 |
* V newValue = (oldValue == null) ? value : |
|
1136 |
* remappingFunction.apply(oldValue, value); |
|
1137 |
* if (newValue == null) |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1138 |
* map.remove(key); |
16867 | 1139 |
* else |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1140 |
* map.put(key, newValue); |
16867 | 1141 |
* }</pre> |
1142 |
* |
|
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1143 |
* <p>The default implementation makes no guarantees about synchronization |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1144 |
* or atomicity properties of this method. Any implementation providing |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1145 |
* atomicity guarantees must override this method and document its |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1146 |
* concurrency properties. In particular, all implementations of |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1147 |
* subinterface {@link java.util.concurrent.ConcurrentMap} must document |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1148 |
* whether the function is applied once atomically only if the value is not |
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1149 |
* present. |
16867 | 1150 |
* |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1151 |
* @param key key with which the resulting value is to be associated |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1152 |
* @param value the non-null value to be merged with the existing value |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1153 |
* associated with the key or, if no existing value or a null value |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1154 |
* is associated with the key, to be associated with the key |
16867 | 1155 |
* @param remappingFunction the function to recompute a value if present |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1156 |
* @return the new value associated with the specified key, or null if no |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1157 |
* value is associated with the key |
16867 | 1158 |
* @throws UnsupportedOperationException if the {@code put} operation |
1159 |
* is not supported by this map |
|
1160 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
1161 |
* @throws ClassCastException if the class of the specified key or value |
|
1162 |
* prevents it from being stored in this map |
|
1163 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
|
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1164 |
* @throws NullPointerException if the specified key is null and this map |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1165 |
* does not support null keys or the value or remappingFunction is |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1166 |
* null |
16867 | 1167 |
* @since 1.8 |
1168 |
*/ |
|
1169 |
default V merge(K key, V value, |
|
1170 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
|
20189
1e618f2a82d9
8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents:
19855
diff
changeset
|
1171 |
Objects.requireNonNull(remappingFunction); |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1172 |
Objects.requireNonNull(value); |
16867 | 1173 |
V oldValue = get(key); |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1174 |
V newValue = (oldValue == null) ? value : |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1175 |
remappingFunction.apply(oldValue, value); |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1176 |
if(newValue == null) { |
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1177 |
remove(key); |
21352
0372edc9a995
8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents:
21339
diff
changeset
|
1178 |
} else { |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1179 |
put(key, newValue); |
16867 | 1180 |
} |
22055
d9836bf9992a
8029055: Map.merge implementations should refuse null value param
mduigou
parents:
21352
diff
changeset
|
1181 |
return newValue; |
16867 | 1182 |
} |
2 | 1183 |
} |