jdk/src/java.base/share/classes/java/util/Map.java
author smarks
Fri, 06 May 2016 11:33:32 -0700
changeset 37802 91fc9ac7b8b3
parent 35396 c58a744d0bb4
child 38567 6d4c5a278fff
permissions -rw-r--r--
8139233: add initial compact immutable collection implementations Reviewed-by: plevart, forax, dfuchs, chegar, alanb, scolebourne
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
     2
 * Copyright (c) 1997, 2015, 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
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    37
 * <p>This interface takes the place of the {@code Dictionary} class, which
2
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
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    40
 * <p>The {@code Map} interface provides three <i>collection views</i>, which
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    44
 * elements.  Some map implementations, like the {@code TreeMap} class, make
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    45
 * specific guarantees as to their order; others, like the {@code HashMap}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    50
 * changed in a manner that affects {@code equals} comparisons while the
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    54
 * advised: the {@code equals} and {@code hashCode} methods are no longer
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    59
 * empty map, and a constructor with a single argument of type {@code Map},
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    68
 * {@code UnsupportedOperationException} if this map does not support the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * operation.  If this is the case, these methods may, but are not required
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    70
 * to, throw an {@code UnsupportedOperationException} if the invocation would
2
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,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    79
 * typically {@code NullPointerException} or {@code ClassCastException}.
2
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)
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    92
 * containsKey(Object key)} method says: "returns {@code true} if and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    93
 * only if this map contains a mapping for a key {@code k} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    94
 * {@code (key==null ? k==null : key.equals(k))}." This specification should
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    95
 * <i>not</i> be construed to imply that invoking {@code Map.containsKey}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    96
 * with a non-null argument {@code key} will cause {@code key.equals(k)} to
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    97
 * be invoked for any key {@code k}.  Implementations are free to
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    98
 * implement optimizations whereby the {@code equals} invocation is avoided,
2
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
 *
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   113
 * <h2><a name="immutable">Immutable Map Static Factory Methods</a></h2>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   114
 * <p>The {@link Map#of() Map.of()} and
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   115
 * {@link Map#ofEntries(Map.Entry...) Map.ofEntries()}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   116
 * static factory methods provide a convenient way to create immutable maps.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   117
 * The {@code Map}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   118
 * instances created by these methods have the following characteristics:
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   119
 *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   120
 * <ul>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   121
 * <li>They are <em>structurally immutable</em>. Keys and values cannot be added,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   122
 * removed, or updated. Attempts to do so result in {@code UnsupportedOperationException}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   123
 * However, if the contained keys or values are themselves mutable, this may cause the
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   124
 * Map to behave inconsistently or its contents to appear to change.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   125
 * <li>They disallow {@code null} keys and values. Attempts to create them with
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   126
 * {@code null} keys or values result in {@code NullPointerException}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   127
 * <li>They are serializable if all keys and values are serializable.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   128
 * <li>They reject duplicate keys at creation time. Duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   129
 * passed to a static factory method result in {@code IllegalArgumentException}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   130
 * <li>The iteration order of mappings is unspecified and is subject to change.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   131
 * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   132
 * Callers should make no assumptions about the identity of the returned instances.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   133
 * Factories are free to create new instances or reuse existing ones. Therefore,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   134
 * identity-sensitive operations on these instances (reference equality ({@code ==}),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   135
 * identity hash code, and synchronization) are unreliable and should be avoided.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   136
 * </ul>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   137
 *
20491
eb2dfc7436af 7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents: 20189
diff changeset
   138
 * <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
   139
 * <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
   140
 * Java Collections Framework</a>.
eb2dfc7436af 7057785: Add note about optional support of recursive methods for self-referential Collection/Map
mduigou
parents: 20189
diff changeset
   141
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @see HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @see TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * @see Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * @see SortedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * @see Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 */
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   154
public interface Map<K, V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the number of key-value mappings in this map.  If the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   159
     * map contains more than {@code Integer.MAX_VALUE} elements, returns
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   160
     * {@code Integer.MAX_VALUE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    int size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   167
     * Returns {@code true} if this map contains no key-value mappings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   169
     * @return {@code true} if this map contains no key-value mappings
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    boolean isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   174
     * Returns {@code true} if this map contains a mapping for the specified
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   175
     * key.  More formally, returns {@code true} if and only if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   176
     * this map contains a mapping for a key {@code k} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   177
     * {@code Objects.equals(key, k)}.  (There can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param key key whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   181
     * @return {@code true} if this map contains a mapping for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @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
   184
     *         this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   185
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @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
   187
     *         does not permit null keys
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   188
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    boolean containsKey(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   193
     * Returns {@code true} if this map maps one or more keys to the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   194
     * specified value.  More formally, returns {@code true} if and only if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   195
     * this map contains at least one mapping to a value {@code v} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   196
     * {@code Objects.equals(value, v)}.  This operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * will probably require time linear in the map size for most
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   198
     * implementations of the {@code Map} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param value value whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   201
     * @return {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @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
   204
     *         this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   205
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @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
   207
     *         map does not permit null values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   208
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    boolean containsValue(Object value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>More formally, if this map contains a mapping from a key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   217
     * {@code k} to a value {@code v} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   218
     * {@code Objects.equals(key, k)},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   219
     * then this method returns {@code v}; otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>If this map permits null values, then a return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * {@code null} does not <i>necessarily</i> indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * contains no mapping for the key; it's also possible that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * explicitly maps the key to {@code null}.  The {@link #containsKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * containsKey} operation may be used to distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param key the key whose associated value is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the value to which the specified key is mapped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         {@code null} if this map contains no mapping for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @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
   232
     *         this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   233
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @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
   235
     *         does not permit null keys
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   236
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    V get(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Associates the specified value with the specified key in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * (optional operation).  If the map previously contained a mapping for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * the key, the old value is replaced by the specified value.  (A map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   246
     * {@code m} is said to contain a mapping for a key {@code k} if and only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * if {@link #containsKey(Object) m.containsKey(k)} would return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   248
     * {@code true}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param value value to be associated with the specified key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   252
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   253
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   254
     *         (A {@code null} return can also indicate that the map
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   255
     *         previously associated {@code null} with {@code key},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   256
     *         if the implementation supports {@code null} values.)
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   257
     * @throws UnsupportedOperationException if the {@code put} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws ClassCastException if the class of the specified key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *         prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         and this map does not permit null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws IllegalArgumentException if some property of the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         or value prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    V put(K key, V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Removes the mapping for a key from this map if it is present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * (optional operation).   More formally, if this map contains a mapping
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   271
     * from key {@code k} to value {@code v} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   272
     * {@code Objects.equals(key, k)}, that mapping
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * is removed.  (The map can contain at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>Returns the value to which this map previously associated the key,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   276
     * or {@code null} if the map contained no mapping for the key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>If this map permits null values, then a return value of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   279
     * {@code null} does not <i>necessarily</i> indicate that the map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * contained no mapping for the key; it's also possible that the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   281
     * explicitly mapped the key to {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>The map will not contain a mapping for the specified key once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param key key whose mapping is to be removed from the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   287
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   288
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   289
     * @throws UnsupportedOperationException if the {@code remove} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @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
   292
     *         this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   293
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @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
   295
     *         map does not permit null keys
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   296
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    V remove(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Copies all of the mappings from the specified map to this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * (optional operation).  The effect of this call is equivalent to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * of calling {@link #put(Object,Object) put(k, v)} on this map once
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   307
     * for each mapping from key {@code k} to value {@code v} in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * specified map.  The behavior of this operation is undefined if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * specified map is modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param m mappings to be stored in this map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   312
     * @throws UnsupportedOperationException if the {@code putAll} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws ClassCastException if the class of a key or value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *         specified map prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws NullPointerException if the specified map is null, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         this map does not permit null keys or values, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         specified map contains null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws IllegalArgumentException if some property of a key or value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *         the specified map prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    void putAll(Map<? extends K, ? extends V> m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Removes all of the mappings from this map (optional operation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   328
     * @throws UnsupportedOperationException if the {@code clear} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    void clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   341
     * the iterator's own {@code remove} operation), the results of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * which removes the corresponding mapping from the map, via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   344
     * {@code Iterator.remove}, {@code Set.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   345
     * {@code removeAll}, {@code retainAll}, and {@code clear}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   346
     * operations.  It does not support the {@code add} or {@code addAll}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return a set view of the keys contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    Set<K> keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * modified while an iteration over the collection is in progress
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   358
     * (except through the iterator's own {@code remove} operation),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   361
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   362
     * {@code Collection.remove}, {@code removeAll},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   363
     * {@code retainAll} and {@code clear} operations.  It does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   364
     * support the {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return a collection view of the values contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    Collection<V> values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   375
     * the iterator's own {@code remove} operation, or through the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   376
     * {@code setValue} operation on a map entry returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   379
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   380
     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   381
     * {@code clear} operations.  It does not support the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   382
     * {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    Set<Map.Entry<K, V>> entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   389
     * A map entry (key-value pair).  The {@code Map.entrySet} method returns
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * a collection-view of the map, whose elements are of this class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <i>only</i> way to obtain a reference to a map entry is from the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   392
     * iterator of this collection-view.  These {@code Map.Entry} objects are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * valid <i>only</i> for the duration of the iteration; more formally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * the behavior of a map entry is undefined if the backing map has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * modified after the entry was returned by the iterator, except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   396
     * the {@code setValue} operation on the map entry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see Map#entrySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   401
    interface Entry<K, V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * Returns the key corresponding to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * @return the key corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        K getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * Returns the value corresponding to this entry.  If the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * has been removed from the backing map (by the iterator's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   415
         * {@code remove} operation), the results of this call are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * @return the value corresponding to this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        V getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * Replaces the value corresponding to this entry with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * value (optional operation).  (Writes through to the map.)  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * behavior of this call is undefined if the mapping has already been
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   428
         * removed from the map (by the iterator's {@code remove} operation).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * @param value new value to be stored in this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * @return old value corresponding to the entry
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   432
         * @throws UnsupportedOperationException if the {@code put} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *         is not supported by the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * @throws ClassCastException if the class of the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         *         prevents it from being stored in the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * @throws NullPointerException if the backing map does not permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         *         null values, and the specified value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * @throws IllegalArgumentException if some property of this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         *         prevents it from being stored in the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * @throws IllegalStateException implementations may, but are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         *         required to, throw this exception if the entry has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         *         removed from the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        V setValue(V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * Compares the specified object with this entry for equality.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   448
         * Returns {@code true} if the given object is also a map entry and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * the two entries represent the same mapping.  More formally, two
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   450
         * entries {@code e1} and {@code e2} represent the same mapping
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * if<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         *     (e1.getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &amp;&amp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         *     (e1.getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * </pre>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   457
         * This ensures that the {@code equals} method works properly across
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   458
         * different implementations of the {@code Map.Entry} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * @param o object to be compared for equality with this map entry
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   461
         * @return {@code true} if the specified object is equal to this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         *         entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        boolean equals(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * Returns the hash code value for this map entry.  The hash code
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   468
         * of a map entry {@code e} is defined to be: <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         *     (e.getValue()==null ? 0 : e.getValue().hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * </pre>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   472
         * This ensures that {@code e1.equals(e2)} implies that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   473
         * {@code e1.hashCode()==e2.hashCode()} for any two Entries
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   474
         * {@code e1} and {@code e2}, as required by the general
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   475
         * contract of {@code Object.hashCode}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * @return the hash code value for this map entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * @see Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int hashCode();
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   483
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   484
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   485
         * Returns a comparator that compares {@link Map.Entry} in natural order on key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   486
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   487
         * <p>The returned comparator is serializable and throws {@link
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   488
         * NullPointerException} when comparing an entry with a null key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   489
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   490
         * @param  <K> the {@link Comparable} type of then map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   491
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   492
         * @return a comparator that compares {@link Map.Entry} in natural order on key.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   493
         * @see Comparable
20868
dcad3ccb889a 8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents: 20491
diff changeset
   494
         * @since 1.8
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   495
         */
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   496
        public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K, V>> comparingByKey() {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   497
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   498
                (c1, c2) -> c1.getKey().compareTo(c2.getKey());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   499
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   500
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   501
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   502
         * Returns a comparator that compares {@link Map.Entry} in natural order on value.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   503
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   504
         * <p>The returned comparator is serializable and throws {@link
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   505
         * NullPointerException} when comparing an entry with null values.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   506
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   507
         * @param <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   508
         * @param <V> the {@link Comparable} type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   509
         * @return a comparator that compares {@link Map.Entry} in natural order on value.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   510
         * @see Comparable
20868
dcad3ccb889a 8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents: 20491
diff changeset
   511
         * @since 1.8
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   512
         */
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
   513
        public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K, V>> comparingByValue() {
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   514
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   515
                (c1, c2) -> c1.getValue().compareTo(c2.getValue());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   516
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   517
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   518
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   519
         * Returns a comparator that compares {@link Map.Entry} by key using the given
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   520
         * {@link Comparator}.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   521
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   522
         * <p>The returned comparator is serializable if the specified comparator
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   523
         * is also serializable.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   524
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   525
         * @param  <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   526
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   527
         * @param  cmp the key {@link Comparator}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   528
         * @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
   529
         * @since 1.8
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   530
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   531
        public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   532
            Objects.requireNonNull(cmp);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   533
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   534
                (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   535
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   536
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   537
        /**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   538
         * Returns a comparator that compares {@link Map.Entry} by value using the given
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   539
         * {@link Comparator}.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   540
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   541
         * <p>The returned comparator is serializable if the specified comparator
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   542
         * is also serializable.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   543
         *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   544
         * @param  <K> the type of the map keys
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   545
         * @param  <V> the type of the map values
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   546
         * @param  cmp the value {@link Comparator}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   547
         * @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
   548
         * @since 1.8
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   549
         */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   550
        public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   551
            Objects.requireNonNull(cmp);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   552
            return (Comparator<Map.Entry<K, V>> & Serializable)
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   553
                (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18532
diff changeset
   554
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Compares the specified object with this map for equality.  Returns
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   561
     * {@code true} if the given object is also a map and the two maps
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   562
     * represent the same mappings.  More formally, two maps {@code m1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   563
     * {@code m2} represent the same mappings if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   564
     * {@code m1.entrySet().equals(m2.entrySet())}.  This ensures that the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   565
     * {@code equals} method works properly across different implementations
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   566
     * of the {@code Map} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param o object to be compared for equality with this map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   569
     * @return {@code true} if the specified object is equal to this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    boolean equals(Object o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * defined to be the sum of the hash codes of each entry in the map's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   576
     * {@code entrySet()} view.  This ensures that {@code m1.equals(m2)}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   577
     * implies that {@code m1.hashCode()==m2.hashCode()} for any two maps
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   578
     * {@code m1} and {@code m2}, as required by the general contract of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @see Map.Entry#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    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
   587
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   588
    // Defaultable methods
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   589
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   590
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   591
     * Returns the value to which the specified key is mapped, or
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   592
     * {@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
   593
     *
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
   594
     * @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
   595
     * 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
   596
     * 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
   597
     * 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
   598
     * concurrency properties.
dcad3ccb889a 8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents: 20491
diff changeset
   599
     *
dcad3ccb889a 8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents: 20491
diff changeset
   600
     * @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
   601
     * @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
   602
     * @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
   603
     * {@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
   604
     * @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
   605
     * this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   606
     * (<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
   607
     * @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
   608
     * does not permit null keys
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   609
     * (<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
   610
     * @since 1.8
dcad3ccb889a 8026768: java.util.Map.Entry comparingBy methods missing @since 1.8
henryjen
parents: 20491
diff changeset
   611
     */
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   612
    default V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   613
        V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   614
        return (((v = get(key)) != null) || containsKey(key))
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   615
            ? v
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   616
            : defaultValue;
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
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   619
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   620
     * Performs the given action for each entry in this map until all entries
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   621
     * have been processed or the action throws an exception.   Unless
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   622
     * otherwise specified by the implementing class, actions are performed in
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   623
     * 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
   624
     * Exceptions thrown by the action are relayed to the caller.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   625
     *
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
   626
     * @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
   627
     * The default implementation is equivalent to, for this {@code map}:
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   628
     * <pre> {@code
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
   629
     * for (Map.Entry<K, V> entry : map.entrySet())
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   630
     *     action.accept(entry.getKey(), entry.getValue());
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   631
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   632
     *
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
   633
     * 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
   634
     * 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
   635
     * 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
   636
     * 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
   637
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   638
     * @param action The action to be performed for each entry
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   639
     * @throws NullPointerException if the specified action is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   640
     * @throws ConcurrentModificationException if an entry is found to be
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   641
     * removed during iteration
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   642
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   643
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   644
    default void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   645
        Objects.requireNonNull(action);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   646
        for (Map.Entry<K, V> entry : entrySet()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   647
            K k;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   648
            V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   649
            try {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   650
                k = entry.getKey();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   651
                v = entry.getValue();
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   652
            } catch (IllegalStateException ise) {
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   653
                // 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
   654
                throw new ConcurrentModificationException(ise);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   655
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   656
            action.accept(k, v);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   657
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   658
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   659
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   660
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   661
     * Replaces each entry's value with the result of invoking the given
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   662
     * function on that entry until all entries have been processed or the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   663
     * function throws an exception.  Exceptions thrown by the function are
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20868
diff changeset
   664
     * relayed to the caller.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   665
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   666
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   667
     * <p>The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   668
     * <pre> {@code
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
   669
     * for (Map.Entry<K, V> entry : map.entrySet())
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   670
     *     entry.setValue(function.apply(entry.getKey(), entry.getValue()));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   671
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   672
     *
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
   673
     * <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
   674
     * 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
   675
     * 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
   676
     * 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
   677
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   678
     * @param function the function to apply to each entry
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   679
     * @throws UnsupportedOperationException if the {@code set} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   680
     * is not supported by this map's entry set iterator.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   681
     * @throws ClassCastException if the class of a replacement value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   682
     * prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   683
     * @throws NullPointerException if the specified function is null, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   684
     * 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
   685
     * values
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   686
     * @throws ClassCastException if a replacement value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   687
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   688
     *         (<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
   689
     * @throws NullPointerException if function or a replacement value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   690
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   691
     *         (<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
   692
     * @throws IllegalArgumentException if some property of a replacement value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   693
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   694
     *         (<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
   695
     * @throws ConcurrentModificationException if an entry is found to be
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   696
     * removed during iteration
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   697
     * @since 1.8
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
    default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   700
        Objects.requireNonNull(function);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   701
        for (Map.Entry<K, V> entry : entrySet()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   702
            K k;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   703
            V v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   704
            try {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   705
                k = entry.getKey();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   706
                v = entry.getValue();
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   707
            } catch (IllegalStateException ise) {
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   708
                // 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
   709
                throw new ConcurrentModificationException(ise);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   710
            }
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   711
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   712
            // 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
   713
            v = function.apply(k, v);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   714
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   715
            try {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   716
                entry.setValue(v);
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   717
            } catch (IllegalStateException ise) {
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   718
                // 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
   719
                throw new ConcurrentModificationException(ise);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 16867
diff changeset
   720
            }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   721
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   722
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   723
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   724
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   725
     * 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
   726
     * to {@code null}) associates it with the given value and returns
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   727
     * {@code null}, else returns the current value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   728
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   729
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   730
     * The default implementation is equivalent to, for this {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   731
     * map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   732
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   733
     * <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
   734
     * 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
   735
     * 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
   736
     *     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
   737
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   738
     * return v;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   739
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   740
     *
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
   741
     * <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
   742
     * 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
   743
     * 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
   744
     * 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
   745
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   746
     * @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
   747
     * @param value value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   748
     * @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
   749
     *         {@code null} if there was no mapping for the key.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   750
     *         (A {@code null} return can also indicate that the map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   751
     *         previously associated {@code null} with the key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   752
     *         if the implementation supports null values.)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   753
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   754
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   755
     *         (<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
   756
     * @throws ClassCastException if the key or value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   757
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   758
     *         (<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
   759
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   760
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   761
     *         (<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
   762
     * @throws IllegalArgumentException if some property of the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   763
     *         or value prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   764
     *         (<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
   765
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   766
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   767
    default V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   768
        V v = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   769
        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
   770
            v = put(key, value);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   771
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   772
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   773
        return v;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   774
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   775
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   776
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   777
     * 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
   778
     * mapped to the specified value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   779
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   780
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   781
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   782
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   783
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   784
     * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   785
     *     map.remove(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   786
     *     return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   787
     * } else
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
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   790
     *
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
   791
     * <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
   792
     * 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
   793
     * 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
   794
     * 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
   795
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   796
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   797
     * @param value value expected to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   798
     * @return {@code true} if the value was removed
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   799
     * @throws UnsupportedOperationException if the {@code remove} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   800
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   801
     *         (<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
   802
     * @throws ClassCastException if the key or value is of an inappropriate
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   803
     *         type for this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   804
     *         (<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
   805
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   806
     *         and this map does not permit null keys or values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   807
     *         (<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
   808
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   809
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   810
    default boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   811
        Object curValue = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   812
        if (!Objects.equals(curValue, value) ||
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   813
            (curValue == null && !containsKey(key))) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   814
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   815
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   816
        remove(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   817
        return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   818
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   819
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   820
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   821
     * Replaces the entry for the specified key only if currently
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   822
     * mapped to the specified value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   823
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   824
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   825
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   826
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   827
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   828
     * if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   829
     *     map.put(key, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   830
     *     return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   831
     * } else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   832
     *     return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   833
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   834
     *
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   835
     * The default implementation does not throw NullPointerException
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   836
     * for maps that do not support null values if oldValue is null unless
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   837
     * newValue is also null.
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   838
     *
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
   839
     * <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
   840
     * 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
   841
     * 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
   842
     * 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
   843
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   844
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   845
     * @param oldValue value expected to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   846
     * @param newValue value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   847
     * @return {@code true} if the value was replaced
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   848
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   849
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   850
     *         (<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
   851
     * @throws ClassCastException if the class of a specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   852
     *         prevents it from being stored in this map
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   853
     * @throws NullPointerException if a specified key or newValue is null,
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   854
     *         and this map does not permit null keys or values
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   855
     * @throws NullPointerException if oldValue is null and this map does not
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19074
diff changeset
   856
     *         permit null values
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   857
     *         (<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
   858
     * @throws IllegalArgumentException if some property of a specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   859
     *         or value prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   860
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   861
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   862
    default boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   863
        Object curValue = get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   864
        if (!Objects.equals(curValue, oldValue) ||
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   865
            (curValue == null && !containsKey(key))) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   866
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   867
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   868
        put(key, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   869
        return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   870
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   871
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   872
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   873
     * Replaces the entry for the specified key only if it is
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   874
     * currently mapped to some value.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   875
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   876
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   877
     * The default implementation is equivalent to, for this {@code map}:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   878
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   879
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   880
     * if (map.containsKey(key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   881
     *     return map.put(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   882
     * } else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   883
     *     return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   884
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   885
     *
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
   886
     * <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
   887
     * 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
   888
     * 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
   889
     * concurrency properties.
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   890
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   891
     * @param key key with which the specified value is associated
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   892
     * @param value value to be associated with the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   893
     * @return the previous value associated with the specified key, or
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   894
     *         {@code null} if there was no mapping for the key.
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   895
     *         (A {@code null} return can also indicate that the map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   896
     *         previously associated {@code null} with the key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   897
     *         if the implementation supports null values.)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   898
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   899
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   900
     *         (<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
   901
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   902
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   903
     *         (<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
   904
     * @throws NullPointerException if the specified key or value is null,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   905
     *         and this map does not permit null keys or values
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   906
     * @throws IllegalArgumentException if some property of the specified key
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   907
     *         or value prevents it from being stored in this map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   908
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   909
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   910
    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
   911
        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
   912
        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
   913
            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
   914
        }
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   915
        return curValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   916
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   917
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   918
    /**
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
   919
     * 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
   920
     * 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
   921
     * function and enters it into this map unless {@code null}.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   922
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   923
     * <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
   924
     * If the mapping function itself throws an (unchecked) exception, the
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   925
     * exception is rethrown, and no mapping is recorded.  The most
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   926
     * 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
   927
     * mapped value or memoized result, as in:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   928
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   929
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   930
     * map.computeIfAbsent(key, k -> new Value(f(k)));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   931
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   932
     *
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
   933
     * <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
   934
     * 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
   935
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   936
     * <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
   937
     * 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
   938
     * }</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
   939
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   940
     * <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
   941
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   942
     * @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
   943
     * 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
   944
     * {@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
   945
     * 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
   946
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
   947
     * <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
   948
     * 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
   949
     *     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
   950
     *     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
   951
     *         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
   952
     * }
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
     * }</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
   954
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   955
     * <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
   956
     * 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
   957
     * 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
   958
     * 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
   959
     * {@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
   960
     * 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
   961
     * 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
   962
     * 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
   963
     * 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
   964
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
   965
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   966
     * <p>The default implementation makes no guarantees about synchronization
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   967
     * or atomicity properties of this method. Any implementation providing
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   968
     * atomicity guarantees must override this method and document its
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   969
     * concurrency properties. In particular, all implementations of
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   970
     * 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
   971
     * 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
   972
     * is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   973
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   974
     * @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
   975
     * @param mappingFunction the mapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   976
     * @return the current (existing or computed) value associated with
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   977
     *         the specified key, or null if the computed value is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   978
     * @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
   979
     *         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
   980
     *         is null
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   981
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   982
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   983
     *         (<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
   984
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   985
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
   986
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   987
     * @throws IllegalArgumentException if some property of the specified key
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   988
     *         or value prevents it from being stored in this map
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
   989
     *         (<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
   990
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   991
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   992
    default V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
   993
            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
   994
        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
   995
        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
   996
        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
   997
            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
   998
            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
   999
                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
  1000
                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
  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
        }
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1003
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1004
        return v;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1005
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1006
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1007
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1008
     * 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
  1009
     * 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
  1010
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1011
     * <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
  1012
     * 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
  1013
     * 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
  1014
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1015
     * <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
  1016
     *
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
  1017
     * @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
  1018
     * 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
  1019
     * 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
  1020
     * {@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
  1021
     *
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1022
     * <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
  1023
     * 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
  1024
     *     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
  1025
     *     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
  1026
     *     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
  1027
     *         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
  1028
     *     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
  1029
     *         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
  1030
     * }
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 21339
diff changeset
  1031
     * }</pre>
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1032
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1033
     * <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
  1034
     * 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
  1035
     * 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
  1036
     * 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
  1037
     * {@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
  1038
     * 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
  1039
     * 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
  1040
     * 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
  1041
     * 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
  1042
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1043
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1044
     * <p>The default implementation makes no guarantees about synchronization
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1045
     * or atomicity properties of this method. Any implementation providing
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1046
     * atomicity guarantees must override this method and document its
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1047
     * concurrency properties. In particular, all implementations of
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1048
     * 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
  1049
     * 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
  1050
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1051
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1052
     * @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
  1053
     * @param remappingFunction the remapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1054
     * @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
  1055
     * @throws NullPointerException if the specified key is null and
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1056
     *         this map does not support null keys, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1057
     *         remappingFunction is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1058
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1059
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1060
     *         (<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
  1061
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1062
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1063
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1064
     * @throws IllegalArgumentException if some property of the specified key
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1065
     *         or value prevents it from being stored in this map
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1066
     *         (<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
  1067
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1068
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1069
    default V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1070
            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
  1071
        Objects.requireNonNull(remappingFunction);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1072
        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
  1073
        if ((oldValue = get(key)) != null) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1074
            V newValue = remappingFunction.apply(key, oldValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1075
            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
  1076
                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
  1077
                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
  1078
            } 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
  1079
                remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1080
                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
  1081
            }
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
        } 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
  1083
            return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1084
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1085
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1086
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1087
    /**
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
  1088
     * 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
  1089
     * 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
  1090
     * 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
  1091
     * mapping:
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1092
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1093
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1094
     * 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
  1095
     * (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
  1096
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1097
     * <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
  1098
     * (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
  1099
     * 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
  1100
     * the current mapping is left unchanged.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1101
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1102
     * <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
  1103
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1104
     * @implSpec
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1105
     * The default implementation is equivalent to performing the following
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1106
     * steps for this {@code map}, then returning the current value or
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1107
     * {@code null} if absent:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1108
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1109
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1110
     * V oldValue = map.get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1111
     * V newValue = remappingFunction.apply(key, oldValue);
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1112
     * if (oldValue != null) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1113
     *    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
  1114
     *       map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1115
     *    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
  1116
     *       map.remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1117
     * } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1118
     *    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
  1119
     *       map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1120
     *    else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1121
     *       return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1122
     * }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1123
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1124
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1125
     * <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
  1126
     * 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
  1127
     * 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
  1128
     * 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
  1129
     * {@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
  1130
     * 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
  1131
     * 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
  1132
     * 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
  1133
     * 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
  1134
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1135
     *
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
  1136
     * <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
  1137
     * 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
  1138
     * 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
  1139
     * 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
  1140
     * 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
  1141
     * 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
  1142
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1143
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1144
     * @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
  1145
     * @param remappingFunction the remapping function to compute a value
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1146
     * @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
  1147
     * @throws NullPointerException if the specified key is null and
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1148
     *         this map does not support null keys, or the
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1149
     *         remappingFunction is null
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1150
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1151
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1152
     *         (<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
  1153
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1154
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1155
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1156
     * @throws IllegalArgumentException if some property of the specified key
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1157
     *         or value prevents it from being stored in this map
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1158
     *         (<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
  1159
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1160
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1161
    default V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1162
            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
  1163
        Objects.requireNonNull(remappingFunction);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1164
        V oldValue = get(key);
18532
0bbca0914946 8017088: Map/HashMap.compute() incorrect with key mapping to null value
mduigou
parents: 18280
diff changeset
  1165
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
  1166
        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
  1167
        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
  1168
            // 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
  1169
            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
  1170
                // 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
  1171
                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
  1172
                return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1173
            } 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
  1174
                // 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
  1175
                return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1176
            }
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
  1177
        } 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
  1178
            // 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
  1179
            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
  1180
            return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1181
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1182
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1183
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1184
    /**
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1185
     * 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
  1186
     * 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
  1187
     * 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
  1188
     * remapping function, or removes if the result is {@code null}. This
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1189
     * 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
  1190
     * 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
  1191
     * value mapping:
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1192
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1193
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1194
     * map.merge(key, msg, String::concat)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1195
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1196
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1197
     * <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
  1198
     * 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
  1199
     * 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
  1200
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1201
     * <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
  1202
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1203
     * @implSpec
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1204
     * The default implementation is equivalent to performing the following
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1205
     * 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
  1206
     * {@code null} if absent:
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1207
     *
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1208
     * <pre> {@code
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1209
     * V oldValue = map.get(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1210
     * V newValue = (oldValue == null) ? value :
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1211
     *              remappingFunction.apply(oldValue, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1212
     * 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
  1213
     *     map.remove(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1214
     * 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
  1215
     *     map.put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1216
     * }</pre>
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1217
     *
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1218
     * <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
  1219
     * 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
  1220
     * 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
  1221
     * 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
  1222
     * {@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
  1223
     * 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
  1224
     * 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
  1225
     * 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
  1226
     * 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
  1227
     * computation would never complete.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1228
     *
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
  1229
     * <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
  1230
     * 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
  1231
     * 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
  1232
     * 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
  1233
     * 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
  1234
     * 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
  1235
     * value is not present.
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1236
     *
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1237
     * @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
  1238
     * @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
  1239
     *        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
  1240
     *        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
  1241
     * @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
  1242
     *        present
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1243
     * @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
  1244
     *         value is associated with the key
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1245
     * @throws UnsupportedOperationException if the {@code put} operation
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1246
     *         is not supported by this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1247
     *         (<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
  1248
     * @throws ClassCastException if the class of the specified key or value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1249
     *         prevents it from being stored in this map
23738
098d1470822e 8039527: Broken links in ConcurrentMap javadoc
chegar
parents: 22055
diff changeset
  1250
     *         (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1251
     * @throws IllegalArgumentException if some property of the specified key
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1252
     *         or value prevents it from being stored in this map
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1253
     *         (<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
  1254
     * @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
  1255
     *         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
  1256
     *         null
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1257
     * @since 1.8
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1258
     */
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1259
    default V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1260
            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
  1261
        Objects.requireNonNull(remappingFunction);
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1262
        Objects.requireNonNull(value);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1263
        V oldValue = get(key);
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1264
        V newValue = (oldValue == null) ? value :
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1265
                   remappingFunction.apply(oldValue, value);
35396
c58a744d0bb4 8145164: Default implementation of ConcurrentMap::compute can throw NPE
dl
parents: 34682
diff changeset
  1266
        if (newValue == null) {
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1267
            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
  1268
        } else {
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1269
            put(key, newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1270
        }
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 21352
diff changeset
  1271
        return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 14342
diff changeset
  1272
    }
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1273
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1274
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1275
     * Returns an immutable map containing zero mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1276
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1277
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1278
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1279
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1280
     * @return an empty {@code Map}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1281
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1282
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1283
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1284
    static <K, V> Map<K, V> of() {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1285
        return new ImmutableCollections.Map0<>();
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1286
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1287
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1288
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1289
     * Returns an immutable map containing a single mapping.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1290
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1291
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1292
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1293
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1294
     * @param k1 the mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1295
     * @param v1 the mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1296
     * @return a {@code Map} containing the specified mapping
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1297
     * @throws NullPointerException if the key or the value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1298
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1299
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1300
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1301
    static <K, V> Map<K, V> of(K k1, V v1) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1302
        return new ImmutableCollections.Map1<>(k1, v1);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1303
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1304
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1305
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1306
     * Returns an immutable map containing two mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1307
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1308
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1309
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1310
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1311
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1312
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1313
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1314
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1315
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1316
     * @throws IllegalArgumentException if the keys are duplicates
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1317
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1318
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1319
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1320
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1321
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1322
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1323
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1324
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1325
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1326
     * Returns an immutable map containing three mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1327
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1328
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1329
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1330
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1331
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1332
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1333
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1334
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1335
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1336
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1337
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1338
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1339
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1340
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1341
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1342
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1343
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1344
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1345
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1346
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1347
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1348
     * Returns an immutable map containing four mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1349
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1350
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1351
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1352
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1353
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1354
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1355
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1356
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1357
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1358
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1359
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1360
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1361
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1362
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1363
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1364
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1365
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1366
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1367
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1368
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1369
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1370
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1371
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1372
     * Returns an immutable map containing five mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1373
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1374
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1375
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1376
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1377
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1378
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1379
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1380
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1381
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1382
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1383
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1384
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1385
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1386
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1387
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1388
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1389
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1390
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1391
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1392
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1393
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1394
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1395
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1396
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1397
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1398
     * Returns an immutable map containing six mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1399
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1400
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1401
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1402
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1403
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1404
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1405
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1406
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1407
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1408
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1409
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1410
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1411
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1412
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1413
     * @param k6 the sixth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1414
     * @param v6 the sixth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1415
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1416
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1417
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1418
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1419
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1420
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1421
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1422
                               K k6, V v6) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1423
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1424
                                               k6, v6);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1425
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1426
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1427
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1428
     * Returns an immutable map containing seven mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1429
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1430
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1431
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1432
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1433
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1434
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1435
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1436
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1437
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1438
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1439
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1440
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1441
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1442
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1443
     * @param k6 the sixth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1444
     * @param v6 the sixth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1445
     * @param k7 the seventh mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1446
     * @param v7 the seventh mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1447
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1448
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1449
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1450
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1451
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1452
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1453
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1454
                               K k6, V v6, K k7, V v7) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1455
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1456
                                               k6, v6, k7, v7);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1457
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1458
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1459
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1460
     * Returns an immutable map containing eight mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1461
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1462
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1463
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1464
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1465
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1466
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1467
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1468
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1469
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1470
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1471
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1472
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1473
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1474
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1475
     * @param k6 the sixth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1476
     * @param v6 the sixth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1477
     * @param k7 the seventh mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1478
     * @param v7 the seventh mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1479
     * @param k8 the eighth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1480
     * @param v8 the eighth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1481
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1482
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1483
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1484
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1485
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1486
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1487
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1488
                               K k6, V v6, K k7, V v7, K k8, V v8) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1489
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1490
                                               k6, v6, k7, v7, k8, v8);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1491
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1492
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1493
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1494
     * Returns an immutable map containing nine mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1495
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1496
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1497
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1498
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1499
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1500
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1501
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1502
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1503
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1504
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1505
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1506
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1507
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1508
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1509
     * @param k6 the sixth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1510
     * @param v6 the sixth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1511
     * @param k7 the seventh mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1512
     * @param v7 the seventh mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1513
     * @param k8 the eighth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1514
     * @param v8 the eighth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1515
     * @param k9 the ninth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1516
     * @param v9 the ninth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1517
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1518
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1519
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1520
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1521
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1522
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1523
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1524
                               K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1525
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1526
                                               k6, v6, k7, v7, k8, v8, k9, v9);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1527
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1528
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1529
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1530
     * Returns an immutable map containing ten mappings.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1531
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1532
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1533
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1534
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1535
     * @param k1 the first mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1536
     * @param v1 the first mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1537
     * @param k2 the second mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1538
     * @param v2 the second mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1539
     * @param k3 the third mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1540
     * @param v3 the third mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1541
     * @param k4 the fourth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1542
     * @param v4 the fourth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1543
     * @param k5 the fifth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1544
     * @param v5 the fifth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1545
     * @param k6 the sixth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1546
     * @param v6 the sixth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1547
     * @param k7 the seventh mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1548
     * @param v7 the seventh mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1549
     * @param k8 the eighth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1550
     * @param v8 the eighth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1551
     * @param k9 the ninth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1552
     * @param v9 the ninth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1553
     * @param k10 the tenth mapping's key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1554
     * @param v10 the tenth mapping's value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1555
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1556
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1557
     * @throws NullPointerException if any key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1558
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1559
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1560
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1561
    static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1562
                               K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1563
        return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1564
                                               k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1565
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1566
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1567
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1568
     * Returns an immutable map containing keys and values extracted from the given entries.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1569
     * The entries themselves are not stored in the map.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1570
     * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1571
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1572
     * @apiNote
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1573
     * It is convenient to create the map entries using the {@link Map#entry Map.entry()} method.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1574
     * For example,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1575
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1576
     * <pre>{@code
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1577
     *     import static java.util.Map.entry;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1578
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1579
     *     Map<Integer,String> map = Map.ofEntries(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1580
     *         entry(1, "a"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1581
     *         entry(2, "b"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1582
     *         entry(3, "c"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1583
     *         ...
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1584
     *         entry(26, "z"));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1585
     * }</pre>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1586
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1587
     * @param <K> the {@code Map}'s key type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1588
     * @param <V> the {@code Map}'s value type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1589
     * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1590
     * @return a {@code Map} containing the specified mappings
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1591
     * @throws IllegalArgumentException if there are any duplicate keys
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1592
     * @throws NullPointerException if any entry, key, or value is {@code null}, or if
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1593
     *         the {@code entries} array is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1594
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1595
     * @see Map#entry Map.entry()
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1596
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1597
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1598
    @SafeVarargs
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1599
    @SuppressWarnings("varargs")
34682
fbee345d8615 8144952: add wildcards to the Map.ofEntries() method
smarks
parents: 34527
diff changeset
  1600
    static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
37802
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1601
        Objects.requireNonNull(entries);
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1602
        if (entries.length == 0) {
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1603
            return new ImmutableCollections.Map0<>();
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1604
        } else if (entries.length == 1) {
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1605
            return new ImmutableCollections.Map1<>(entries[0].getKey(),
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1606
                                                   entries[0].getValue());
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1607
        } else {
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1608
            Object[] kva = new Object[entries.length << 1];
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1609
            int a = 0;
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1610
            for (Entry<? extends K, ? extends V> entry : entries) {
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1611
                kva[a++] = entry.getKey();
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1612
                kva[a++] = entry.getValue();
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1613
            }
91fc9ac7b8b3 8139233: add initial compact immutable collection implementations
smarks
parents: 35396
diff changeset
  1614
            return new ImmutableCollections.MapN<>(kva);
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1615
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1616
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1617
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1618
    /**
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1619
     * Returns an immutable {@link Entry} containing the given key and value.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1620
     * These entries are suitable for populating {@code Map} instances using the
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1621
     * {@link Map#ofEntries Map.ofEntries()} method.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1622
     * The {@code Entry} instances created by this method have the following characteristics:
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1623
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1624
     * <ul>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1625
     * <li>They disallow {@code null} keys and values. Attempts to create them using a {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1626
     * key or value result in {@code NullPointerException}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1627
     * <li>They are immutable. Calls to {@link Entry#setValue Entry.setValue()}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1628
     * on a returned {@code Entry} result in {@code UnsupportedOperationException}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1629
     * <li>They are not serializable.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1630
     * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1631
     * Callers should make no assumptions about the identity of the returned instances.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1632
     * This method is free to create new instances or reuse existing ones. Therefore,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1633
     * identity-sensitive operations on these instances (reference equality ({@code ==}),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1634
     * identity hash code, and synchronization) are unreliable and should be avoided.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1635
     * </ul>
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1636
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1637
     * @apiNote
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1638
     * For a serializable {@code Entry}, see {@link AbstractMap.SimpleEntry} or
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1639
     * {@link AbstractMap.SimpleImmutableEntry}.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1640
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1641
     * @param <K> the key's type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1642
     * @param <V> the value's type
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1643
     * @param k the key
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1644
     * @param v the value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1645
     * @return an {@code Entry} containing the specified key and value
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1646
     * @throws NullPointerException if the key or value is {@code null}
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1647
     *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1648
     * @see Map#ofEntries Map.ofEntries()
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1649
     * @since 9
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1650
     */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1651
    static <K, V> Entry<K, V> entry(K k, V v) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1652
        // KeyValueHolder checks for nulls
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1653
        return new KeyValueHolder<>(k, v);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents: 32108
diff changeset
  1654
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
}