< prev index next >

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

Print this page
rev 48077 : 8193128: Reduce number of implementation classes returned by List/Set/Map.of()
Reviewed-by: smarks


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.Map0.instance();
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.Map0.instance();
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.




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 >