jdk/src/java.base/share/classes/java/util/Map.java
author bchristi
Thu, 02 Apr 2015 12:33:03 -0700
changeset 29743 981893a47bec
parent 25859 3317bb8137f4
child 32108 aa5490a167ee
permissions -rw-r--r--
8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find. Summary: Throw ConcurrentModificationException from computeIfAbsent() & friends Reviewed-by: chegar, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    28
import java.util.function.BiConsumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    29
import java.util.function.BiFunction;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    30
import java.util.function.Function;
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
    31
import java.io.Serializable;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An object that maps keys to values.  A map cannot contain duplicate keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * each key can map to at most one value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>This interface takes the place of the <tt>Dictionary</tt> class, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * was a totally abstract class rather than an interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>The <tt>Map</tt> interface provides three <i>collection views</i>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * allow a map's contents to be viewed as a set of keys, collection of values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * or set of key-value mappings.  The <i>order</i> of a map is defined as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the order in which the iterators on the map's collection views return their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * elements.  Some map implementations, like the <tt>TreeMap</tt> class, make
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * specific guarantees as to their order; others, like the <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * class, do not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Note: great care must be exercised if mutable objects are used as map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * keys.  The behavior of a map is not specified if the value of an object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * changed in a manner that affects <tt>equals</tt> comparisons while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * object is a key in the map.  A special case of this prohibition is that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is not permissible for a map to contain itself as a key.  While it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * permissible for a map to contain itself as a value, extreme caution is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * advised: the <tt>equals</tt> and <tt>hashCode</tt> methods are no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * well defined on such a map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>All general-purpose map implementation classes should provide two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * "standard" constructors: a void (no arguments) constructor which creates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * empty map, and a constructor with a single argument of type <tt>Map</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * which creates a new map with the same key-value mappings as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * In effect, the latter constructor allows the user to copy any map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * producing an equivalent map of the desired class.  There is no way to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * enforce this recommendation (as interfaces cannot contain constructors) but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * all of the general-purpose map implementations in the JDK comply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>The "destructive" methods contained in this interface, that is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * methods that modify the map on which they operate, are specified to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tt>UnsupportedOperationException</tt> if this map does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * operation.  If this is the case, these methods may, but are not required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * to, throw an <tt>UnsupportedOperationException</tt> if the invocation would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * have no effect on the map.  For example, invoking the {@link #putAll(Map)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * method on an unmodifiable map may, but is not required to, throw the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exception if the map whose mappings are to be "superimposed" is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>Some map implementations have restrictions on the keys and values they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * may contain.  For example, some implementations prohibit null keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * values, and some have restrictions on the types of their keys.  Attempting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * to insert an ineligible key or value throws an unchecked exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * typically <tt>NullPointerException</tt> or <tt>ClassCastException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Attempting to query the presence of an ineligible key or value may throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * exception, or it may simply return false; some implementations will exhibit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * the former behavior and some will exhibit the latter.  More generally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * attempting an operation on an ineligible key or value whose completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * would not result in the insertion of an ineligible element into the map may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * throw an exception or it may succeed, at the option of the implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Such exceptions are marked as "optional" in the specification for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>Many methods in Collections Framework interfaces are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * in terms of the {@link Object#equals(Object) equals} method.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * example, the specification for the {@link #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * containsKey(Object key)} method says: "returns <tt>true</tt> if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * only if this map contains a mapping for a key <tt>k</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <tt>(key==null ? k==null : key.equals(k))</tt>." This specification should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <i>not</i> be construed to imply that invoking <tt>Map.containsKey</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * with a non-null argument <tt>key</tt> will cause <tt>key.equals(k)</tt> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * be invoked for any key <tt>k</tt>.  Implementations are free to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * implement optimizations whereby the <tt>equals</tt> invocation is avoided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * for example, by first comparing the hash codes of the two keys.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * {@link Object#hashCode()} specification guarantees that two objects with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * unequal hash codes cannot be equal.)  More generally, implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * the various Collections Framework interfaces are free to take advantage of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * the specified behavior of underlying {@link Object} methods wherever the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * implementor deems it appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @see HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @see TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see SortedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
public interface Map<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the number of key-value mappings in this map.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <tt>Integer.MAX_VALUE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    int size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return <tt>true</tt> if this map contains no key-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    boolean isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * key.  More formally, returns <tt>true</tt> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * this map contains a mapping for a key <tt>k</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <tt>(key==null ? k==null : key.equals(k))</tt>.  (There can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param key key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   160
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   163
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    boolean containsKey(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * specified value.  More formally, returns <tt>true</tt> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * this map contains at least one mapping to a value <tt>v</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <tt>(value==null ? v==null : value.equals(v))</tt>.  This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * will probably require time linear in the map size for most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * implementations of the <tt>Map</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   180
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   183
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    boolean containsValue(Object value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>If this map permits null values, then a return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * {@code null} does not <i>necessarily</i> indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * contains no mapping for the key; it's also possible that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * explicitly maps the key to {@code null}.  The {@link #containsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * containsKey} operation may be used to distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param key the key whose associated value is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the value to which the specified key is mapped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         {@code null} if this map contains no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   207
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   210
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    V get(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Associates the specified value with the specified key in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * (optional operation).  If the map previously contained a mapping for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the key, the old value is replaced by the specified value.  (A map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * if {@link #containsKey(Object) m.containsKey(k)} would return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <tt>true</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         previously associated <tt>null</tt> with <tt>key</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         if the implementation supports <tt>null</tt> values.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws UnsupportedOperationException if the <tt>put</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws ClassCastException if the class of the specified key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *         prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *         and this map does not permit null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @throws IllegalArgumentException if some property of the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         or value prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    V put(K key, V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Removes the mapping for a key from this map if it is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * (optional operation).   More formally, if this map contains a mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * from key <tt>k</tt> to value <tt>v</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <code>(key==null ?  k==null : key.equals(k))</code>, that mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * is removed.  (The map can contain at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p>Returns the value to which this map previously associated the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * or <tt>null</tt> if the map contained no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>If this map permits null values, then a return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <tt>null</tt> does not <i>necessarily</i> indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * contained no mapping for the key; it's also possible that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * explicitly mapped the key to <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>The map will not contain a mapping for the specified key once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   267
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   270
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    V remove(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Copies all of the mappings from the specified map to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * (optional operation).  The effect of this call is equivalent to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * of calling {@link #put(Object,Object) put(k, v)} on this map once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * specified map.  The behavior of this operation is undefined if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * specified map is modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws ClassCastException if the class of a key or value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *         specified map prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws NullPointerException if the specified map is null, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         this map does not permit null keys or values, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *         specified map contains null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @throws IllegalArgumentException if some property of a key or value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *         the specified map prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void putAll(Map<? extends K, ? extends V> m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Removes all of the mappings from this map (optional operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @throws UnsupportedOperationException if the <tt>clear</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    void clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return a set view of the keys contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    Set<K> keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return a collection view of the values contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    Collection<V> values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    Set<Map.Entry<K, V>> entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * A map entry (key-value pair).  The <tt>Map.entrySet</tt> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * a collection-view of the map, whose elements are of this class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <i>only</i> way to obtain a reference to a map entry is from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * iterator of this collection-view.  These <tt>Map.Entry</tt> objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * valid <i>only</i> for the duration of the iteration; more formally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * the behavior of a map entry is undefined if the backing map has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * modified after the entry was returned by the iterator, except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the <tt>setValue</tt> operation on the map entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see Map#entrySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    interface Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        K getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * Returns the value corresponding to this entry.  If the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * has been removed from the backing map (by the iterator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * <tt>remove</tt> operation), the results of this call are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        V getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * value (optional operation).  (Writes through to the map.)  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * behavior of this call is undefined if the mapping has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * removed from the map (by the iterator's <tt>remove</tt> operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * @return old value corresponding to the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * @throws UnsupportedOperationException if the <tt>put</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         *         is not supported by the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * @throws ClassCastException if the class of the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         *         prevents it from being stored in the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * @throws NullPointerException if the backing map does not permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         *         null values, and the specified value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * @throws IllegalArgumentException if some property of this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         *         prevents it from being stored in the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        V setValue(V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Compares the specified object with this entry for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * Returns <tt>true</tt> if the given object is also a map entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * the two entries represent the same mapping.  More formally, two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         *     (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         *     (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * This ensures that the <tt>equals</tt> method works properly across
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         * different implementations of the <tt>Map.Entry</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * @param o object to be compared for equality with this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        boolean equals(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Returns the hash code value for this map entry.  The hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * of a map entry <tt>e</tt> is defined to be: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         *     (e.getValue()==null ? 0 : e.getValue().hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * This ensures that <tt>e1.equals(e2)</tt> implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * <tt>e1</tt> and <tt>e2</tt>, as required by the general
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * contract of <tt>Object.hashCode</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * @see Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int hashCode();
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   457
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   458
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   459
         * Returns a comparator that compares {@link Map.Entry} in natural order on key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   460
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   461
         * <p>The returned comparator is serializable and throws {@link
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   462
         * NullPointerException} when comparing an entry with a null key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   463
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   464
         * @param  <K> the {@link Comparable} type of then map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   465
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   466
         * @return a comparator that compares {@link Map.Entry} in natural order on key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   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
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   469
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   470
        public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   471
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   472
                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   473
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   474
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   475
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   476
         * Returns a comparator that compares {@link Map.Entry} in natural order on value.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   477
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   478
         * <p>The returned comparator is serializable and throws {@link
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   479
         * NullPointerException} when comparing an entry with null values.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   480
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   481
         * @param <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   482
         * @param <V> the {@link Comparable} type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   483
         * @return a comparator that compares {@link Map.Entry} in natural order on value.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   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
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   486
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   487
        public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   488
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   489
                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   490
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   491
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   492
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   493
         * Returns a comparator that compares {@link Map.Entry} by key using the given
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   494
         * {@link Comparator}.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   495
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   496
         * <p>The returned comparator is serializable if the specified comparator
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   497
         * is also serializable.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   498
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   499
         * @param  <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   500
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   501
         * @param  cmp the key {@link Comparator}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   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
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   504
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   505
        public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   506
            Objects.requireNonNull(cmp);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   507
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   508
                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   509
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   510
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   511
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   512
         * Returns a comparator that compares {@link Map.Entry} by value using the given
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   513
         * {@link Comparator}.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   514
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   515
         * <p>The returned comparator is serializable if the specified comparator
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   516
         * is also serializable.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   517
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   518
         * @param  <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   519
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   520
         * @param  cmp the value {@link Comparator}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   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
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   523
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   524
        public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   525
            Objects.requireNonNull(cmp);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   526
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   527
                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   528
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Compares the specified object with this map for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <tt>true</tt> if the given object is also a map and the two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * represent the same mappings.  More formally, two maps <tt>m1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <tt>m2</tt> represent the same mappings if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * <tt>equals</tt> method works properly across different implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * of the <tt>Map</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param o object to be compared for equality with this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return <tt>true</tt> if the specified object is equal to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    boolean equals(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * defined to be the sum of the hash codes of each entry in the map's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @see Map.Entry#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   562
    // Defaultable methods
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   563
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   564
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   565
     * Returns the value to which the specified key is mapped, or
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   580
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
20868
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
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   583
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
20868
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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   586
    default V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   587
        V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   588
        return (((v = get(key)) != null) || containsKey(key))
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   589
            ? v
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   590
            : defaultValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   591
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   592
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   593
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   594
     * Performs the given action for each entry in this map until all entries
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   595
     * have been processed or the action throws an exception.   Unless
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   596
     * otherwise specified by the implementing class, actions are performed in
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   597
     * the order of entry set iteration (if an iteration order is specified.)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   598
     * Exceptions thrown by the action are relayed to the caller.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   604
     *     action.accept(entry.getKey(), entry.getValue());
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   605
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   612
     * @param action The action to be performed for each entry
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   613
     * @throws NullPointerException if the specified action is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   614
     * @throws ConcurrentModificationException if an entry is found to be
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   615
     * removed during iteration
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   616
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   617
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   618
    default void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   619
        Objects.requireNonNull(action);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   620
        for (Map.Entry<K, V> entry : entrySet()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   621
            K k;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   622
            V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   623
            try {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   624
                k = entry.getKey();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   625
                v = entry.getValue();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   628
                throw new ConcurrentModificationException(ise);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   629
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   630
            action.accept(k, v);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   631
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   632
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   633
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   634
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   635
     * Replaces each entry's value with the result of invoking the given
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   636
     * function on that entry until all entries have been processed or the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   637
     * function throws an exception.  Exceptions thrown by the function are
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   638
     * relayed to the caller.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   639
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   640
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   641
     * <p>The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   644
     *     entry.setValue(function.apply(entry.getKey(), entry.getValue()));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   645
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   652
     * @param function the function to apply to each entry
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   653
     * @throws UnsupportedOperationException if the {@code set} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   654
     * is not supported by this map's entry set iterator.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   655
     * @throws ClassCastException if the class of a replacement value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   656
     * prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   657
     * @throws NullPointerException if the specified function is null, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   658
     * specified replacement value is null, and this map does not permit null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   659
     * values
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   660
     * @throws ClassCastException if a replacement value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   661
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   662
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   663
     * @throws NullPointerException if function or a replacement value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   664
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   665
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   666
     * @throws IllegalArgumentException if some property of a replacement value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   667
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   668
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   669
     * @throws ConcurrentModificationException if an entry is found to be
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   670
     * removed during iteration
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   671
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   672
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   673
    default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   674
        Objects.requireNonNull(function);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   675
        for (Map.Entry<K, V> entry : entrySet()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   676
            K k;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   677
            V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   678
            try {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   679
                k = entry.getKey();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   680
                v = entry.getValue();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   683
                throw new ConcurrentModificationException(ise);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   695
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   696
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   697
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   698
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   699
     * If the specified key is not already associated with a value (or is mapped
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   700
     * to {@code null}) associates it with the given value and returns
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   701
     * {@code null}, else returns the current value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   702
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   703
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   704
     * The default implementation is equivalent to, for this {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   705
     * map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   706
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   713
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   720
     * @param key key with which the specified value is to be associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   721
     * @param value value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   724
     *         (A {@code null} return can also indicate that the map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   725
     *         previously associated {@code null} with the key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   726
     *         if the implementation supports null values.)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   727
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   728
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   729
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   730
     * @throws ClassCastException if the key or value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   731
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   732
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   733
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   734
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   735
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   736
     * @throws IllegalArgumentException if some property of the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   737
     *         or value prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   738
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   739
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   740
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   741
    default V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   742
        V v = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   745
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   746
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   747
        return v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   748
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   749
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   750
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   751
     * Removes the entry for the specified key only if it is currently
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   752
     * mapped to the specified value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   753
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   754
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   755
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   756
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   757
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   758
     * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   759
     *     map.remove(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   760
     *     return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   761
     * } else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   762
     *     return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   763
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   770
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   771
     * @param value value expected to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   772
     * @return {@code true} if the value was removed
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   773
     * @throws UnsupportedOperationException if the {@code remove} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   774
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   775
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   776
     * @throws ClassCastException if the key or value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   777
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   778
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   779
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   780
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   781
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   782
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   783
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   784
    default boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   785
        Object curValue = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   786
        if (!Objects.equals(curValue, value) ||
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   787
            (curValue == null && !containsKey(key))) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   788
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   789
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   790
        remove(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   791
        return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   792
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   793
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   794
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   795
     * Replaces the entry for the specified key only if currently
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   796
     * mapped to the specified value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   797
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   798
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   799
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   800
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   801
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   802
     * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   803
     *     map.put(key, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   804
     *     return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   805
     * } else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   806
     *     return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   807
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   808
     *
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   809
     * The default implementation does not throw NullPointerException
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   810
     * for maps that do not support null values if oldValue is null unless
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   811
     * newValue is also null.
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   818
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   819
     * @param oldValue value expected to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   820
     * @param newValue value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   821
     * @return {@code true} if the value was replaced
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   822
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   823
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   824
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   825
     * @throws ClassCastException if the class of a specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   826
     *         prevents it from being stored in this map
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   827
     * @throws NullPointerException if a specified key or newValue is null,
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   828
     *         and this map does not permit null keys or values
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   829
     * @throws NullPointerException if oldValue is null and this map does not
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   830
     *         permit null values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   831
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   832
     * @throws IllegalArgumentException if some property of a specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   833
     *         or value prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   834
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   835
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   836
    default boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   837
        Object curValue = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   838
        if (!Objects.equals(curValue, oldValue) ||
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   839
            (curValue == null && !containsKey(key))) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   840
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   841
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   842
        put(key, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   843
        return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   844
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   845
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   846
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   847
     * Replaces the entry for the specified key only if it is
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   848
     * currently mapped to some value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   849
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   850
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   851
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   852
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   853
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   854
     * if (map.containsKey(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   855
     *     return map.put(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   856
     * } else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   857
     *     return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   858
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   865
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   866
     * @param value value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   867
     * @return the previous value associated with the specified key, or
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   868
     *         {@code null} if there was no mapping for the key.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   869
     *         (A {@code null} return can also indicate that the map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   870
     *         previously associated {@code null} with the key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   871
     *         if the implementation supports null values.)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   872
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   873
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   874
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   875
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   876
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   877
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   878
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   879
     *         and this map does not permit null keys or values
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   880
     * @throws IllegalArgumentException if some property of the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   881
     *         or value prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   882
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   883
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   890
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   891
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   896
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   897
     * <p>If the mapping function returns {@code null}, no mapping is recorded.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   898
     * If the mapping function itself throws an (unchecked) exception, the
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   899
     * exception is rethrown, and no mapping is recorded.  The most
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   900
     * common usage is to construct a new object serving as an initial
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   901
     * mapped value or memoized result, as in:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   902
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   903
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   904
     * map.computeIfAbsent(key, k -> new Value(f(k)));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   905
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   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
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   914
     * <p>The mapping function should not modify this map during computation.
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
   915
     *
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
     * @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
   917
     * 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
   918
     * {@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
   919
     * 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
   920
     *
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
     * <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
   922
     * 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
   923
     *     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
   924
     *     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
   925
     *         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
   926
     * }
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
     * }</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
   928
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   929
     * <p>The default implementation makes no guarantees about detecting if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   930
     * mapping function modifies this map during computation and, if
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   931
     * appropriate, reporting an error. Non-concurrent implementations should
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   932
     * override this method and, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   933
     * {@code ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   934
     * mapping function modifies this map during computation. Concurrent
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   935
     * implementations should override this method and, on a best-effort basis,
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   936
     * throw an {@code IllegalStateException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   937
     * mapping function modifies this map during computation and as a result
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   938
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   939
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   940
     * <p>The default implementation makes no guarantees about synchronization
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   941
     * or atomicity properties of this method. Any implementation providing
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   942
     * atomicity guarantees must override this method and document its
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   943
     * concurrency properties. In particular, all implementations of
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   944
     * subinterface {@link java.util.concurrent.ConcurrentMap} must document
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   945
     * whether the mapping function is applied once atomically only if the value
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   946
     * is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   947
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   948
     * @param key key with which the specified value is to be associated
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   949
     * @param mappingFunction the mapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   950
     * @return the current (existing or computed) value associated with
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   951
     *         the specified key, or null if the computed value is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   952
     * @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
   953
     *         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
   954
     *         is null
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   955
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   956
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   957
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   958
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   959
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   960
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   961
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   962
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   963
    default V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   964
            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
   965
        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
   966
        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
   967
        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
   968
            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
   969
            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
   970
                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
   971
                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
   972
            }
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
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
        return v;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   976
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   977
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   978
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   979
     * If the value for the specified key is present and non-null, attempts to
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   980
     * compute a new mapping given the key and its current mapped value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   981
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   982
     * <p>If the remapping function returns {@code null}, the mapping is removed.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   983
     * If the remapping function itself throws an (unchecked) exception, the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   984
     * exception is rethrown, and the current mapping is left unchanged.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   985
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   986
     * <p>The remapping function should not modify this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   987
     *
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
   988
     * @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
   989
     * 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
   990
     * 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
   991
     * {@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
   992
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   993
     * <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
   994
     * 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
   995
     *     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
   996
     *     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
   997
     *     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
   998
     *         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
   999
     *     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
  1000
     *         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
  1001
     * }
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1002
     * }</pre>
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1003
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1004
     * <p>The default implementation makes no guarantees about detecting if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1005
     * remapping function modifies this map during computation and, if
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1006
     * appropriate, reporting an error. Non-concurrent implementations should
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1007
     * override this method and, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1008
     * {@code ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1009
     * remapping function modifies this map during computation. Concurrent
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1010
     * implementations should override this method and, on a best-effort basis,
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1011
     * throw an {@code IllegalStateException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1012
     * remapping function modifies this map during computation and as a result
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1013
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1014
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1015
     * <p>The default implementation makes no guarantees about synchronization
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1016
     * or atomicity properties of this method. Any implementation providing
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1017
     * atomicity guarantees must override this method and document its
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1018
     * concurrency properties. In particular, all implementations of
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1019
     * subinterface {@link java.util.concurrent.ConcurrentMap} must document
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1020
     * whether the remapping function is applied once atomically only if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1021
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1022
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1023
     * @param key key with which the specified value is to be associated
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1024
     * @param remappingFunction the remapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1025
     * @return the new value associated with the specified key, or null if none
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1026
     * @throws NullPointerException if the specified key is null and
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1027
     *         this map does not support null keys, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1028
     *         remappingFunction is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1029
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1030
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1031
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1032
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1033
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1034
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1035
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1036
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1037
    default V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1038
            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
  1039
        Objects.requireNonNull(remappingFunction);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1040
        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
  1041
        if ((oldValue = get(key)) != null) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1042
            V newValue = remappingFunction.apply(key, oldValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1043
            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
  1044
                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
  1045
                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
  1046
            } 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
  1047
                remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1048
                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
  1049
            }
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1050
        } 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
  1051
            return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1052
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1053
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1054
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1055
    /**
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
  1056
     * 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
  1057
     * 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
  1058
     * 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
  1059
     * mapping:
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1060
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1061
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1062
     * map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))}</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1063
     * (Method {@link #merge merge()} is often simpler to use for such purposes.)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1064
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1065
     * <p>If the remapping function returns {@code null}, the mapping is removed
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1066
     * (or remains absent if initially absent).  If the remapping function
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1067
     * itself throws an (unchecked) exception, the exception is rethrown, and
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1068
     * the current mapping is left unchanged.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1069
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1070
     * <p>The remapping function should not modify this map during computation.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1071
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1072
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1073
     * The default implementation is equivalent to performing the following
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1074
     * steps for this {@code map}, then returning the current value or
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1075
     * {@code null} if absent:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1076
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1077
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1078
     * V oldValue = map.get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1079
     * V newValue = remappingFunction.apply(key, oldValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1080
     * if (oldValue != null ) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1081
     *    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
  1082
     *       map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1083
     *    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
  1084
     *       map.remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1085
     * } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1086
     *    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
  1087
     *       map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1088
     *    else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1089
     *       return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1090
     * }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1091
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1092
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1093
     * <p>The default implementation makes no guarantees about detecting if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1094
     * remapping function modifies this map during computation and, if
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1095
     * appropriate, reporting an error. Non-concurrent implementations should
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1096
     * override this method and, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1097
     * {@code ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1098
     * remapping function modifies this map during computation. Concurrent
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1099
     * implementations should override this method and, on a best-effort basis,
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1100
     * throw an {@code IllegalStateException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1101
     * remapping function modifies this map during computation and as a result
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1102
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  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
     * <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
  1105
     * 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
  1106
     * 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
  1107
     * 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
  1108
     * subinterface {@link java.util.concurrent.ConcurrentMap} must document
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1109
     * whether the remapping function is applied once atomically only if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1110
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1111
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1112
     * @param key key with which the specified value is to be associated
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1113
     * @param remappingFunction the remapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1114
     * @return the new value associated with the specified key, or null if none
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1115
     * @throws NullPointerException if the specified key is null and
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1116
     *         this map does not support null keys, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1117
     *         remappingFunction is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1118
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1119
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1120
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1121
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1122
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1123
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1124
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1125
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1126
    default V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1127
            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
  1128
        Objects.requireNonNull(remappingFunction);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1129
        V oldValue = get(key);
18532
0bbca0914946 8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents: 18280
diff changeset
  1130
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
  1131
        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
  1132
        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
  1133
            // 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
  1134
            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
  1135
                // 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
  1136
                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
  1137
                return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1138
            } 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
  1139
                // 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
  1140
                return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1141
            }
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
  1142
        } 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
  1143
            // 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
  1144
            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
  1145
            return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1146
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1147
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1148
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1149
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1150
     * 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
  1151
     * 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
  1152
     * Otherwise, replaces the associated value with the results of the given
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1153
     * remapping function, or removes if the result is {@code null}. This
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1154
     * method may be of use when combining multiple mapped values for a key.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1155
     * For example, to either create or append a {@code String msg} to a
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1156
     * value mapping:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1157
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1158
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1159
     * map.merge(key, msg, String::concat)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1160
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1161
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1162
     * <p>If the remapping function returns {@code null}, the mapping is removed.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1163
     * If the remapping function itself throws an (unchecked) exception, the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1164
     * exception is rethrown, and the current mapping is left unchanged.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1165
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1166
     * <p>The remapping function should not modify this map during computation.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1167
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1168
     * @implSpec
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1169
     * The default implementation is equivalent to performing the following
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1170
     * 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
  1171
     * {@code null} if absent:
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1172
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1173
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1174
     * V oldValue = map.get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1175
     * V newValue = (oldValue == null) ? value :
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1176
     *              remappingFunction.apply(oldValue, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1177
     * 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
  1178
     *     map.remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1179
     * 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
  1180
     *     map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1181
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1182
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1183
     * <p>The default implementation makes no guarantees about detecting if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1184
     * remapping function modifies this map during computation and, if
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1185
     * appropriate, reporting an error. Non-concurrent implementations should
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1186
     * override this method and, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1187
     * {@code ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1188
     * remapping function modifies this map during computation. Concurrent
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1189
     * implementations should override this method and, on a best-effort basis,
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1190
     * throw an {@code IllegalStateException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1191
     * remapping function modifies this map during computation and as a result
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1192
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1193
     *
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
  1194
     * <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
  1195
     * 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
  1196
     * 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
  1197
     * 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
  1198
     * subinterface {@link java.util.concurrent.ConcurrentMap} must document
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1199
     * whether the remapping function is applied once atomically only if the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1200
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1201
     *
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1202
     * @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
  1203
     * @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
  1204
     *        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
  1205
     *        is associated with the key, to be associated with the key
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1206
     * @param remappingFunction the remapping function to recompute a value if
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1207
     *        present
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1208
     * @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
  1209
     *         value is associated with the key
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1210
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1211
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1212
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1213
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1214
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1215
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1216
     * @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
  1217
     *         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
  1218
     *         null
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1219
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1220
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1221
    default V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1222
            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
  1223
        Objects.requireNonNull(remappingFunction);
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1224
        Objects.requireNonNull(value);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1225
        V oldValue = get(key);
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1226
        V newValue = (oldValue == null) ? value :
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1227
                   remappingFunction.apply(oldValue, value);
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1228
        if(newValue == null) {
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1229
            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
  1230
        } else {
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1231
            put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1232
        }
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1233
        return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1234
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
}