< prev index next >

src/java.base/share/classes/java/util/Map.java

Print this page




1269         V newValue = (oldValue == null) ? value :
1270                    remappingFunction.apply(oldValue, value);
1271         if (newValue == null) {
1272             remove(key);
1273         } else {
1274             put(key, newValue);
1275         }
1276         return newValue;
1277     }
1278 
1279     /**
1280      * Returns an unmodifiable map containing zero mappings.
1281      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1282      *
1283      * @param <K> the {@code Map}'s key type
1284      * @param <V> the {@code Map}'s value type
1285      * @return an empty {@code Map}
1286      *
1287      * @since 9
1288      */
1289     @SuppressWarnings("unchecked")
1290     static <K, V> Map<K, V> of() {
1291         return (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
1292     }
1293 
1294     /**
1295      * Returns an unmodifiable map containing a single mapping.
1296      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1297      *
1298      * @param <K> the {@code Map}'s key type
1299      * @param <V> the {@code Map}'s value type
1300      * @param k1 the mapping's key
1301      * @param v1 the mapping's value
1302      * @return a {@code Map} containing the specified mapping
1303      * @throws NullPointerException if the key or the value is {@code null}
1304      *
1305      * @since 9
1306      */
1307     static <K, V> Map<K, V> of(K k1, V v1) {
1308         return new ImmutableCollections.Map1<>(k1, v1);
1309     }
1310 
1311     /**


1588      *         entry(3, "c"),
1589      *         ...
1590      *         entry(26, "z"));
1591      * }</pre>
1592      *
1593      * @param <K> the {@code Map}'s key type
1594      * @param <V> the {@code Map}'s value type
1595      * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated
1596      * @return a {@code Map} containing the specified mappings
1597      * @throws IllegalArgumentException if there are any duplicate keys
1598      * @throws NullPointerException if any entry, key, or value is {@code null}, or if
1599      *         the {@code entries} array is {@code null}
1600      *
1601      * @see Map#entry Map.entry()
1602      * @since 9
1603      */
1604     @SafeVarargs
1605     @SuppressWarnings("varargs")
1606     static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
1607         if (entries.length == 0) { // implicit null check of entries array
1608             @SuppressWarnings("unchecked")
1609             var map = (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
1610             return map;
1611         } else if (entries.length == 1) {
1612             // implicit null check of the array slot
1613             return new ImmutableCollections.Map1<>(entries[0].getKey(),
1614                     entries[0].getValue());
1615         } else {
1616             Object[] kva = new Object[entries.length << 1];
1617             int a = 0;
1618             for (Entry<? extends K, ? extends V> entry : entries) {
1619                 // implicit null checks of each array slot
1620                 kva[a++] = entry.getKey();
1621                 kva[a++] = entry.getValue();
1622             }
1623             return new ImmutableCollections.MapN<>(kva);
1624         }
1625     }
1626 
1627     /**
1628      * Returns an unmodifiable {@link Entry} containing the given key and value.
1629      * These entries are suitable for populating {@code Map} instances using the
1630      * {@link Map#ofEntries Map.ofEntries()} method.




1269         V newValue = (oldValue == null) ? value :
1270                    remappingFunction.apply(oldValue, value);
1271         if (newValue == null) {
1272             remove(key);
1273         } else {
1274             put(key, newValue);
1275         }
1276         return newValue;
1277     }
1278 
1279     /**
1280      * Returns an unmodifiable map containing zero mappings.
1281      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1282      *
1283      * @param <K> the {@code Map}'s key type
1284      * @param <V> the {@code Map}'s value type
1285      * @return an empty {@code Map}
1286      *
1287      * @since 9
1288      */

1289     static <K, V> Map<K, V> of() {
1290         return ImmutableCollections.emptyMap();
1291     }
1292 
1293     /**
1294      * Returns an unmodifiable map containing a single mapping.
1295      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1296      *
1297      * @param <K> the {@code Map}'s key type
1298      * @param <V> the {@code Map}'s value type
1299      * @param k1 the mapping's key
1300      * @param v1 the mapping's value
1301      * @return a {@code Map} containing the specified mapping
1302      * @throws NullPointerException if the key or the value is {@code null}
1303      *
1304      * @since 9
1305      */
1306     static <K, V> Map<K, V> of(K k1, V v1) {
1307         return new ImmutableCollections.Map1<>(k1, v1);
1308     }
1309 
1310     /**


1587      *         entry(3, "c"),
1588      *         ...
1589      *         entry(26, "z"));
1590      * }</pre>
1591      *
1592      * @param <K> the {@code Map}'s key type
1593      * @param <V> the {@code Map}'s value type
1594      * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated
1595      * @return a {@code Map} containing the specified mappings
1596      * @throws IllegalArgumentException if there are any duplicate keys
1597      * @throws NullPointerException if any entry, key, or value is {@code null}, or if
1598      *         the {@code entries} array is {@code null}
1599      *
1600      * @see Map#entry Map.entry()
1601      * @since 9
1602      */
1603     @SafeVarargs
1604     @SuppressWarnings("varargs")
1605     static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
1606         if (entries.length == 0) { // implicit null check of entries array
1607             return ImmutableCollections.emptyMap();


1608         } else if (entries.length == 1) {
1609             // implicit null check of the array slot
1610             return new ImmutableCollections.Map1<>(entries[0].getKey(),
1611                     entries[0].getValue());
1612         } else {
1613             Object[] kva = new Object[entries.length << 1];
1614             int a = 0;
1615             for (Entry<? extends K, ? extends V> entry : entries) {
1616                 // implicit null checks of each array slot
1617                 kva[a++] = entry.getKey();
1618                 kva[a++] = entry.getValue();
1619             }
1620             return new ImmutableCollections.MapN<>(kva);
1621         }
1622     }
1623 
1624     /**
1625      * Returns an unmodifiable {@link Entry} containing the given key and value.
1626      * These entries are suitable for populating {@code Map} instances using the
1627      * {@link Map#ofEntries Map.ofEntries()} method.


< prev index next >