< 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     /**
1311      * Returns an unmodifiable map containing two mappings.
1312      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1313      *
1314      * @param <K> the {@code Map}'s key type
1315      * @param <V> the {@code Map}'s value type
1316      * @param k1 the first mapping's key
1317      * @param v1 the first mapping's value
1318      * @param k2 the second mapping's key
1319      * @param v2 the second mapping's value
1320      * @return a {@code Map} containing the specified mappings
1321      * @throws IllegalArgumentException if the keys are duplicates
1322      * @throws NullPointerException if any key or value is {@code null}
1323      *
1324      * @since 9
1325      */
1326     static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
1327         return new ImmutableCollections.MapN<>(k1, v1, k2, v2);


1586      *         entry(2, "b"),
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.
1628      * The {@code Entry} instances created by this method have the following characteristics:
1629      *
1630      * <ul>
1631      * <li>They disallow {@code null} keys and values. Attempts to create them using a {@code null}
1632      * key or value result in {@code NullPointerException}.
1633      * <li>They are unmodifiable. Calls to {@link Entry#setValue Entry.setValue()}
1634      * on a returned {@code Entry} result in {@code UnsupportedOperationException}.
1635      * <li>They are not serializable.
1636      * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
1637      * Callers should make no assumptions about the identity of the returned instances.
1638      * This method is free to create new instances or reuse existing ones. Therefore,
1639      * identity-sensitive operations on these instances (reference equality ({@code ==}),
1640      * identity hash code, and synchronization) are unreliable and should be avoided.
1641      * </ul>




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 ImmutableCollections.AbstractImmutableMap.of(k1, v1);
1308     }
1309 
1310     /**
1311      * Returns an unmodifiable map containing two mappings.
1312      * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
1313      *
1314      * @param <K> the {@code Map}'s key type
1315      * @param <V> the {@code Map}'s value type
1316      * @param k1 the first mapping's key
1317      * @param v1 the first mapping's value
1318      * @param k2 the second mapping's key
1319      * @param v2 the second mapping's value
1320      * @return a {@code Map} containing the specified mappings
1321      * @throws IllegalArgumentException if the keys are duplicates
1322      * @throws NullPointerException if any key or value is {@code null}
1323      *
1324      * @since 9
1325      */
1326     static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
1327         return new ImmutableCollections.MapN<>(k1, v1, k2, v2);


1586      *         entry(2, "b"),
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         return ImmutableCollections.AbstractImmutableMap.ofEntries(entries);















1607     }
1608 
1609     /**
1610      * Returns an unmodifiable {@link Entry} containing the given key and value.
1611      * These entries are suitable for populating {@code Map} instances using the
1612      * {@link Map#ofEntries Map.ofEntries()} method.
1613      * The {@code Entry} instances created by this method have the following characteristics:
1614      *
1615      * <ul>
1616      * <li>They disallow {@code null} keys and values. Attempts to create them using a {@code null}
1617      * key or value result in {@code NullPointerException}.
1618      * <li>They are unmodifiable. Calls to {@link Entry#setValue Entry.setValue()}
1619      * on a returned {@code Entry} result in {@code UnsupportedOperationException}.
1620      * <li>They are not serializable.
1621      * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
1622      * Callers should make no assumptions about the identity of the returned instances.
1623      * This method is free to create new instances or reuse existing ones. Therefore,
1624      * identity-sensitive operations on these instances (reference equality ({@code ==}),
1625      * identity hash code, and synchronization) are unreliable and should be avoided.
1626      * </ul>


< prev index next >