jdk/src/java.base/share/classes/java/util/Map.java
changeset 34527 e3caf3a43d09
parent 32108 aa5490a167ee
child 34682 fbee345d8615
equal deleted inserted replaced
34526:f1f852f5f477 34527:e3caf3a43d09
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   108  * indirectly contains itself. This includes the {@code clone()},
   108  * indirectly contains itself. This includes the {@code clone()},
   109  * {@code equals()}, {@code hashCode()} and {@code toString()} methods.
   109  * {@code equals()}, {@code hashCode()} and {@code toString()} methods.
   110  * Implementations may optionally handle the self-referential scenario, however
   110  * Implementations may optionally handle the self-referential scenario, however
   111  * most current implementations do not do so.
   111  * most current implementations do not do so.
   112  *
   112  *
       
   113  * <h2><a name="immutable">Immutable Map Static Factory Methods</a></h2>
       
   114  * <p>The {@link Map#of() Map.of()} and
       
   115  * {@link Map#ofEntries(Map.Entry...) Map.ofEntries()}
       
   116  * static factory methods provide a convenient way to create immutable maps.
       
   117  * The {@code Map}
       
   118  * instances created by these methods have the following characteristics:
       
   119  *
       
   120  * <ul>
       
   121  * <li>They are <em>structurally immutable</em>. Keys and values cannot be added,
       
   122  * removed, or updated. Attempts to do so result in {@code UnsupportedOperationException}.
       
   123  * However, if the contained keys or values are themselves mutable, this may cause the
       
   124  * Map to behave inconsistently or its contents to appear to change.
       
   125  * <li>They disallow {@code null} keys and values. Attempts to create them with
       
   126  * {@code null} keys or values result in {@code NullPointerException}.
       
   127  * <li>They are serializable if all keys and values are serializable.
       
   128  * <li>They reject duplicate keys at creation time. Duplicate keys
       
   129  * passed to a static factory method result in {@code IllegalArgumentException}.
       
   130  * <li>The iteration order of mappings is unspecified and is subject to change.
       
   131  * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
       
   132  * Callers should make no assumptions about the identity of the returned instances.
       
   133  * Factories are free to create new instances or reuse existing ones. Therefore,
       
   134  * identity-sensitive operations on these instances (reference equality ({@code ==}),
       
   135  * identity hash code, and synchronization) are unreliable and should be avoided.
       
   136  * </ul>
       
   137  *
   113  * <p>This interface is a member of the
   138  * <p>This interface is a member of the
   114  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   139  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   115  * Java Collections Framework</a>.
   140  * Java Collections Framework</a>.
   116  *
   141  *
   117  * @param <K> the type of keys maintained by this map
   142  * @param <K> the type of keys maintained by this map
   124  * @see SortedMap
   149  * @see SortedMap
   125  * @see Collection
   150  * @see Collection
   126  * @see Set
   151  * @see Set
   127  * @since 1.2
   152  * @since 1.2
   128  */
   153  */
   129 public interface Map<K,V> {
   154 public interface Map<K, V> {
   130     // Query Operations
   155     // Query Operations
   131 
   156 
   132     /**
   157     /**
   133      * Returns the number of key-value mappings in this map.  If the
   158      * Returns the number of key-value mappings in this map.  If the
   134      * map contains more than {@code Integer.MAX_VALUE} elements, returns
   159      * map contains more than {@code Integer.MAX_VALUE} elements, returns
   371      * the {@code setValue} operation on the map entry.
   396      * the {@code setValue} operation on the map entry.
   372      *
   397      *
   373      * @see Map#entrySet()
   398      * @see Map#entrySet()
   374      * @since 1.2
   399      * @since 1.2
   375      */
   400      */
   376     interface Entry<K,V> {
   401     interface Entry<K, V> {
   377         /**
   402         /**
   378          * Returns the key corresponding to this entry.
   403          * Returns the key corresponding to this entry.
   379          *
   404          *
   380          * @return the key corresponding to this entry
   405          * @return the key corresponding to this entry
   381          * @throws IllegalStateException implementations may, but are not
   406          * @throws IllegalStateException implementations may, but are not
   466          * @param  <V> the type of the map values
   491          * @param  <V> the type of the map values
   467          * @return a comparator that compares {@link Map.Entry} in natural order on key.
   492          * @return a comparator that compares {@link Map.Entry} in natural order on key.
   468          * @see Comparable
   493          * @see Comparable
   469          * @since 1.8
   494          * @since 1.8
   470          */
   495          */
   471         public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
   496         public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K, V>> comparingByKey() {
   472             return (Comparator<Map.Entry<K, V>> & Serializable)
   497             return (Comparator<Map.Entry<K, V>> & Serializable)
   473                 (c1, c2) -> c1.getKey().compareTo(c2.getKey());
   498                 (c1, c2) -> c1.getKey().compareTo(c2.getKey());
   474         }
   499         }
   475 
   500 
   476         /**
   501         /**
   483          * @param <V> the {@link Comparable} type of the map values
   508          * @param <V> the {@link Comparable} type of the map values
   484          * @return a comparator that compares {@link Map.Entry} in natural order on value.
   509          * @return a comparator that compares {@link Map.Entry} in natural order on value.
   485          * @see Comparable
   510          * @see Comparable
   486          * @since 1.8
   511          * @since 1.8
   487          */
   512          */
   488         public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
   513         public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K, V>> comparingByValue() {
   489             return (Comparator<Map.Entry<K, V>> & Serializable)
   514             return (Comparator<Map.Entry<K, V>> & Serializable)
   490                 (c1, c2) -> c1.getValue().compareTo(c2.getValue());
   515                 (c1, c2) -> c1.getValue().compareTo(c2.getValue());
   491         }
   516         }
   492 
   517 
   493         /**
   518         /**
  1231         } else {
  1256         } else {
  1232             put(key, newValue);
  1257             put(key, newValue);
  1233         }
  1258         }
  1234         return newValue;
  1259         return newValue;
  1235     }
  1260     }
       
  1261 
       
  1262     /**
       
  1263      * Returns an immutable map containing zero mappings.
       
  1264      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1265      *
       
  1266      * @param <K> the {@code Map}'s key type
       
  1267      * @param <V> the {@code Map}'s value type
       
  1268      * @return an empty {@code Map}
       
  1269      *
       
  1270      * @since 9
       
  1271      */
       
  1272     static <K, V> Map<K, V> of() {
       
  1273         return Collections.emptyMap();
       
  1274     }
       
  1275 
       
  1276     /**
       
  1277      * Returns an immutable map containing a single mapping.
       
  1278      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1279      *
       
  1280      * @param <K> the {@code Map}'s key type
       
  1281      * @param <V> the {@code Map}'s value type
       
  1282      * @param k1 the mapping's key
       
  1283      * @param v1 the mapping's value
       
  1284      * @return a {@code Map} containing the specified mapping
       
  1285      * @throws NullPointerException if the key or the value is {@code null}
       
  1286      *
       
  1287      * @since 9
       
  1288      */
       
  1289     static <K, V> Map<K, V> of(K k1, V v1) {
       
  1290         return Collections.singletonMap(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1291     }
       
  1292 
       
  1293     /**
       
  1294      * Returns an immutable map containing two mappings.
       
  1295      * See <a href="#immutable">Immutable Map Static Factory Methods</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 first mapping's key
       
  1300      * @param v1 the first mapping's value
       
  1301      * @param k2 the second mapping's key
       
  1302      * @param v2 the second mapping's value
       
  1303      * @return a {@code Map} containing the specified mappings
       
  1304      * @throws IllegalArgumentException if the keys are duplicates
       
  1305      * @throws NullPointerException if any key or value is {@code null}
       
  1306      *
       
  1307      * @since 9
       
  1308      */
       
  1309     static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
       
  1310         Map<K, V> map = new HashMap<>(3); // specify number of buckets to avoid resizing
       
  1311         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1312         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1313         if (map.size() != 2) {
       
  1314             throw new IllegalArgumentException("duplicate keys");
       
  1315         }
       
  1316         return Collections.unmodifiableMap(map);
       
  1317     }
       
  1318 
       
  1319     /**
       
  1320      * Returns an immutable map containing three mappings.
       
  1321      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1322      *
       
  1323      * @param <K> the {@code Map}'s key type
       
  1324      * @param <V> the {@code Map}'s value type
       
  1325      * @param k1 the first mapping's key
       
  1326      * @param v1 the first mapping's value
       
  1327      * @param k2 the second mapping's key
       
  1328      * @param v2 the second mapping's value
       
  1329      * @param k3 the third mapping's key
       
  1330      * @param v3 the third mapping's value
       
  1331      * @return a {@code Map} containing the specified mappings
       
  1332      * @throws IllegalArgumentException if there are any duplicate keys
       
  1333      * @throws NullPointerException if any key or value is {@code null}
       
  1334      *
       
  1335      * @since 9
       
  1336      */
       
  1337     static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
       
  1338         Map<K, V> map = new HashMap<>(5); // specify number of buckets to avoid resizing
       
  1339         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1340         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1341         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1342         if (map.size() != 3) {
       
  1343             throw new IllegalArgumentException("duplicate keys");
       
  1344         }
       
  1345         return Collections.unmodifiableMap(map);
       
  1346     }
       
  1347 
       
  1348     /**
       
  1349      * Returns an immutable map containing four mappings.
       
  1350      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1351      *
       
  1352      * @param <K> the {@code Map}'s key type
       
  1353      * @param <V> the {@code Map}'s value type
       
  1354      * @param k1 the first mapping's key
       
  1355      * @param v1 the first mapping's value
       
  1356      * @param k2 the second mapping's key
       
  1357      * @param v2 the second mapping's value
       
  1358      * @param k3 the third mapping's key
       
  1359      * @param v3 the third mapping's value
       
  1360      * @param k4 the fourth mapping's key
       
  1361      * @param v4 the fourth mapping's value
       
  1362      * @return a {@code Map} containing the specified mappings
       
  1363      * @throws IllegalArgumentException if there are any duplicate keys
       
  1364      * @throws NullPointerException if any key or value is {@code null}
       
  1365      *
       
  1366      * @since 9
       
  1367      */
       
  1368     static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
       
  1369         Map<K, V> map = new HashMap<>(6); // specify number of buckets to avoid resizing
       
  1370         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1371         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1372         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1373         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1374         if (map.size() != 4) {
       
  1375             throw new IllegalArgumentException("duplicate keys");
       
  1376         }
       
  1377         return Collections.unmodifiableMap(map);
       
  1378     }
       
  1379 
       
  1380     /**
       
  1381      * Returns an immutable map containing five mappings.
       
  1382      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1383      *
       
  1384      * @param <K> the {@code Map}'s key type
       
  1385      * @param <V> the {@code Map}'s value type
       
  1386      * @param k1 the first mapping's key
       
  1387      * @param v1 the first mapping's value
       
  1388      * @param k2 the second mapping's key
       
  1389      * @param v2 the second mapping's value
       
  1390      * @param k3 the third mapping's key
       
  1391      * @param v3 the third mapping's value
       
  1392      * @param k4 the fourth mapping's key
       
  1393      * @param v4 the fourth mapping's value
       
  1394      * @param k5 the fifth mapping's key
       
  1395      * @param v5 the fifth mapping's value
       
  1396      * @return a {@code Map} containing the specified mappings
       
  1397      * @throws IllegalArgumentException if there are any duplicate keys
       
  1398      * @throws NullPointerException if any key or value is {@code null}
       
  1399      *
       
  1400      * @since 9
       
  1401      */
       
  1402     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) {
       
  1403         Map<K, V> map = new HashMap<>(7); // specify number of buckets to avoid resizing
       
  1404         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1405         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1406         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1407         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1408         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1409         if (map.size() != 5) {
       
  1410             throw new IllegalArgumentException("duplicate keys");
       
  1411         }
       
  1412         return Collections.unmodifiableMap(map);
       
  1413     }
       
  1414 
       
  1415     /**
       
  1416      * Returns an immutable map containing six mappings.
       
  1417      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1418      *
       
  1419      * @param <K> the {@code Map}'s key type
       
  1420      * @param <V> the {@code Map}'s value type
       
  1421      * @param k1 the first mapping's key
       
  1422      * @param v1 the first mapping's value
       
  1423      * @param k2 the second mapping's key
       
  1424      * @param v2 the second mapping's value
       
  1425      * @param k3 the third mapping's key
       
  1426      * @param v3 the third mapping's value
       
  1427      * @param k4 the fourth mapping's key
       
  1428      * @param v4 the fourth mapping's value
       
  1429      * @param k5 the fifth mapping's key
       
  1430      * @param v5 the fifth mapping's value
       
  1431      * @param k6 the sixth mapping's key
       
  1432      * @param v6 the sixth mapping's value
       
  1433      * @return a {@code Map} containing the specified mappings
       
  1434      * @throws IllegalArgumentException if there are any duplicate keys
       
  1435      * @throws NullPointerException if any key or value is {@code null}
       
  1436      *
       
  1437      * @since 9
       
  1438      */
       
  1439     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,
       
  1440                                K k6, V v6) {
       
  1441         Map<K, V> map = new HashMap<>(9); // specify number of buckets to avoid resizing
       
  1442         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1443         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1444         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1445         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1446         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1447         map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
       
  1448         if (map.size() != 6) {
       
  1449             throw new IllegalArgumentException("duplicate keys");
       
  1450         }
       
  1451         return Collections.unmodifiableMap(map);
       
  1452     }
       
  1453 
       
  1454     /**
       
  1455      * Returns an immutable map containing seven mappings.
       
  1456      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1457      *
       
  1458      * @param <K> the {@code Map}'s key type
       
  1459      * @param <V> the {@code Map}'s value type
       
  1460      * @param k1 the first mapping's key
       
  1461      * @param v1 the first mapping's value
       
  1462      * @param k2 the second mapping's key
       
  1463      * @param v2 the second mapping's value
       
  1464      * @param k3 the third mapping's key
       
  1465      * @param v3 the third mapping's value
       
  1466      * @param k4 the fourth mapping's key
       
  1467      * @param v4 the fourth mapping's value
       
  1468      * @param k5 the fifth mapping's key
       
  1469      * @param v5 the fifth mapping's value
       
  1470      * @param k6 the sixth mapping's key
       
  1471      * @param v6 the sixth mapping's value
       
  1472      * @param k7 the seventh mapping's key
       
  1473      * @param v7 the seventh mapping's value
       
  1474      * @return a {@code Map} containing the specified mappings
       
  1475      * @throws IllegalArgumentException if there are any duplicate keys
       
  1476      * @throws NullPointerException if any key or value is {@code null}
       
  1477      *
       
  1478      * @since 9
       
  1479      */
       
  1480     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,
       
  1481                                K k6, V v6, K k7, V v7) {
       
  1482         Map<K, V> map = new HashMap<>(10); // specify number of buckets to avoid resizing
       
  1483         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1484         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1485         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1486         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1487         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1488         map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
       
  1489         map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
       
  1490         if (map.size() != 7) {
       
  1491             throw new IllegalArgumentException("duplicate keys");
       
  1492         }
       
  1493         return Collections.unmodifiableMap(map);
       
  1494     }
       
  1495 
       
  1496     /**
       
  1497      * Returns an immutable map containing eight mappings.
       
  1498      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1499      *
       
  1500      * @param <K> the {@code Map}'s key type
       
  1501      * @param <V> the {@code Map}'s value type
       
  1502      * @param k1 the first mapping's key
       
  1503      * @param v1 the first mapping's value
       
  1504      * @param k2 the second mapping's key
       
  1505      * @param v2 the second mapping's value
       
  1506      * @param k3 the third mapping's key
       
  1507      * @param v3 the third mapping's value
       
  1508      * @param k4 the fourth mapping's key
       
  1509      * @param v4 the fourth mapping's value
       
  1510      * @param k5 the fifth mapping's key
       
  1511      * @param v5 the fifth mapping's value
       
  1512      * @param k6 the sixth mapping's key
       
  1513      * @param v6 the sixth mapping's value
       
  1514      * @param k7 the seventh mapping's key
       
  1515      * @param v7 the seventh mapping's value
       
  1516      * @param k8 the eighth mapping's key
       
  1517      * @param v8 the eighth mapping's value
       
  1518      * @return a {@code Map} containing the specified mappings
       
  1519      * @throws IllegalArgumentException if there are any duplicate keys
       
  1520      * @throws NullPointerException if any key or value is {@code null}
       
  1521      *
       
  1522      * @since 9
       
  1523      */
       
  1524     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,
       
  1525                                K k6, V v6, K k7, V v7, K k8, V v8) {
       
  1526         Map<K, V> map = new HashMap<>(11); // specify number of buckets to avoid resizing
       
  1527         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1528         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1529         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1530         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1531         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1532         map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
       
  1533         map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
       
  1534         map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
       
  1535         if (map.size() != 8) {
       
  1536             throw new IllegalArgumentException("duplicate keys");
       
  1537         }
       
  1538         return Collections.unmodifiableMap(map);
       
  1539     }
       
  1540 
       
  1541     /**
       
  1542      * Returns an immutable map containing nine mappings.
       
  1543      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1544      *
       
  1545      * @param <K> the {@code Map}'s key type
       
  1546      * @param <V> the {@code Map}'s value type
       
  1547      * @param k1 the first mapping's key
       
  1548      * @param v1 the first mapping's value
       
  1549      * @param k2 the second mapping's key
       
  1550      * @param v2 the second mapping's value
       
  1551      * @param k3 the third mapping's key
       
  1552      * @param v3 the third mapping's value
       
  1553      * @param k4 the fourth mapping's key
       
  1554      * @param v4 the fourth mapping's value
       
  1555      * @param k5 the fifth mapping's key
       
  1556      * @param v5 the fifth mapping's value
       
  1557      * @param k6 the sixth mapping's key
       
  1558      * @param v6 the sixth mapping's value
       
  1559      * @param k7 the seventh mapping's key
       
  1560      * @param v7 the seventh mapping's value
       
  1561      * @param k8 the eighth mapping's key
       
  1562      * @param v8 the eighth mapping's value
       
  1563      * @param k9 the ninth mapping's key
       
  1564      * @param v9 the ninth mapping's value
       
  1565      * @return a {@code Map} containing the specified mappings
       
  1566      * @throws IllegalArgumentException if there are any duplicate keys
       
  1567      * @throws NullPointerException if any key or value is {@code null}
       
  1568      *
       
  1569      * @since 9
       
  1570      */
       
  1571     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,
       
  1572                                K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
       
  1573         Map<K, V> map = new HashMap<>(13); // specify number of buckets to avoid resizing
       
  1574         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1575         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1576         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1577         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1578         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1579         map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
       
  1580         map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
       
  1581         map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
       
  1582         map.put(Objects.requireNonNull(k9), Objects.requireNonNull(v9));
       
  1583         if (map.size() != 9) {
       
  1584             throw new IllegalArgumentException("duplicate keys");
       
  1585         }
       
  1586         return Collections.unmodifiableMap(map);
       
  1587     }
       
  1588 
       
  1589     /**
       
  1590      * Returns an immutable map containing ten mappings.
       
  1591      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1592      *
       
  1593      * @param <K> the {@code Map}'s key type
       
  1594      * @param <V> the {@code Map}'s value type
       
  1595      * @param k1 the first mapping's key
       
  1596      * @param v1 the first mapping's value
       
  1597      * @param k2 the second mapping's key
       
  1598      * @param v2 the second mapping's value
       
  1599      * @param k3 the third mapping's key
       
  1600      * @param v3 the third mapping's value
       
  1601      * @param k4 the fourth mapping's key
       
  1602      * @param v4 the fourth mapping's value
       
  1603      * @param k5 the fifth mapping's key
       
  1604      * @param v5 the fifth mapping's value
       
  1605      * @param k6 the sixth mapping's key
       
  1606      * @param v6 the sixth mapping's value
       
  1607      * @param k7 the seventh mapping's key
       
  1608      * @param v7 the seventh mapping's value
       
  1609      * @param k8 the eighth mapping's key
       
  1610      * @param v8 the eighth mapping's value
       
  1611      * @param k9 the ninth mapping's key
       
  1612      * @param v9 the ninth mapping's value
       
  1613      * @param k10 the tenth mapping's key
       
  1614      * @param v10 the tenth mapping's value
       
  1615      * @return a {@code Map} containing the specified mappings
       
  1616      * @throws IllegalArgumentException if there are any duplicate keys
       
  1617      * @throws NullPointerException if any key or value is {@code null}
       
  1618      *
       
  1619      * @since 9
       
  1620      */
       
  1621     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,
       
  1622                                K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
       
  1623         Map<K, V> map = new HashMap<>(14); // specify number of buckets to avoid resizing
       
  1624         map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
       
  1625         map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
       
  1626         map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
       
  1627         map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
       
  1628         map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
       
  1629         map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
       
  1630         map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
       
  1631         map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
       
  1632         map.put(Objects.requireNonNull(k9), Objects.requireNonNull(v9));
       
  1633         map.put(Objects.requireNonNull(k10), Objects.requireNonNull(v10));
       
  1634         if (map.size() != 10) {
       
  1635             throw new IllegalArgumentException("duplicate keys");
       
  1636         }
       
  1637         return Collections.unmodifiableMap(map);
       
  1638     }
       
  1639 
       
  1640     /**
       
  1641      * Returns an immutable map containing keys and values extracted from the given entries.
       
  1642      * The entries themselves are not stored in the map.
       
  1643      * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
       
  1644      *
       
  1645      * @apiNote
       
  1646      * It is convenient to create the map entries using the {@link Map#entry Map.entry()} method.
       
  1647      * For example,
       
  1648      *
       
  1649      * <pre>{@code
       
  1650      *     import static java.util.Map.entry;
       
  1651      *
       
  1652      *     Map<Integer,String> map = Map.ofEntries(
       
  1653      *         entry(1, "a"),
       
  1654      *         entry(2, "b"),
       
  1655      *         entry(3, "c"),
       
  1656      *         ...
       
  1657      *         entry(26, "z"));
       
  1658      * }</pre>
       
  1659      *
       
  1660      * @param <K> the {@code Map}'s key type
       
  1661      * @param <V> the {@code Map}'s value type
       
  1662      * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated
       
  1663      * @return a {@code Map} containing the specified mappings
       
  1664      * @throws IllegalArgumentException if there are any duplicate keys
       
  1665      * @throws NullPointerException if any entry, key, or value is {@code null}, or if
       
  1666      *         the {@code entries} array is {@code null}
       
  1667      *
       
  1668      * @see Map#entry Map.entry()
       
  1669      * @since 9
       
  1670      */
       
  1671     @SafeVarargs
       
  1672     @SuppressWarnings("varargs")
       
  1673     static <K, V> Map<K, V> ofEntries(Entry<K, V>... entries) {
       
  1674         Map<K, V> map = new HashMap<>(entries.length * 4 / 3 + 1); // throws NPE if entries is null
       
  1675         for (Entry<K, V> e : entries) {
       
  1676             // next line throws NPE if e is null
       
  1677             map.put(Objects.requireNonNull(e.getKey()), Objects.requireNonNull(e.getValue()));
       
  1678         }
       
  1679         if (map.size() != entries.length) {
       
  1680             throw new IllegalArgumentException("duplicate keys");
       
  1681         }
       
  1682         return Collections.unmodifiableMap(map);
       
  1683     }
       
  1684 
       
  1685     /**
       
  1686      * Returns an immutable {@link Entry} containing the given key and value.
       
  1687      * These entries are suitable for populating {@code Map} instances using the
       
  1688      * {@link Map#ofEntries Map.ofEntries()} method.
       
  1689      * The {@code Entry} instances created by this method have the following characteristics:
       
  1690      *
       
  1691      * <ul>
       
  1692      * <li>They disallow {@code null} keys and values. Attempts to create them using a {@code null}
       
  1693      * key or value result in {@code NullPointerException}.
       
  1694      * <li>They are immutable. Calls to {@link Entry#setValue Entry.setValue()}
       
  1695      * on a returned {@code Entry} result in {@code UnsupportedOperationException}.
       
  1696      * <li>They are not serializable.
       
  1697      * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
       
  1698      * Callers should make no assumptions about the identity of the returned instances.
       
  1699      * This method is free to create new instances or reuse existing ones. Therefore,
       
  1700      * identity-sensitive operations on these instances (reference equality ({@code ==}),
       
  1701      * identity hash code, and synchronization) are unreliable and should be avoided.
       
  1702      * </ul>
       
  1703      *
       
  1704      * @apiNote
       
  1705      * For a serializable {@code Entry}, see {@link AbstractMap.SimpleEntry} or
       
  1706      * {@link AbstractMap.SimpleImmutableEntry}.
       
  1707      *
       
  1708      * @param <K> the key's type
       
  1709      * @param <V> the value's type
       
  1710      * @param k the key
       
  1711      * @param v the value
       
  1712      * @return an {@code Entry} containing the specified key and value
       
  1713      * @throws NullPointerException if the key or value is {@code null}
       
  1714      *
       
  1715      * @see Map#ofEntries Map.ofEntries()
       
  1716      * @since 9
       
  1717      */
       
  1718     static <K, V> Entry<K, V> entry(K k, V v) {
       
  1719         // KeyValueHolder checks for nulls
       
  1720         return new KeyValueHolder<>(k, v);
       
  1721     }
  1236 }
  1722 }