93 * only if this map contains a mapping for a key {@code k} such that
94 * {@code (key==null ? k==null : key.equals(k))}." This specification should
95 * <i>not</i> be construed to imply that invoking {@code Map.containsKey}
96 * with a non-null argument {@code key} will cause {@code key.equals(k)} to
97 * be invoked for any key {@code k}. Implementations are free to
98 * implement optimizations whereby the {@code equals} invocation is avoided,
99 * for example, by first comparing the hash codes of the two keys. (The
100 * {@link Object#hashCode()} specification guarantees that two objects with
101 * unequal hash codes cannot be equal.) More generally, implementations of
102 * the various Collections Framework interfaces are free to take advantage of
103 * the specified behavior of underlying {@link Object} methods wherever the
104 * implementor deems it appropriate.
105 *
106 * <p>Some map operations which perform recursive traversal of the map may fail
107 * with an exception for self-referential instances where the map directly or
108 * indirectly contains itself. This includes the {@code clone()},
109 * {@code equals()}, {@code hashCode()} and {@code toString()} methods.
110 * Implementations may optionally handle the self-referential scenario, however
111 * most current implementations do not do so.
112 *
113 * <p>This interface is a member of the
114 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
115 * Java Collections Framework</a>.
116 *
117 * @param <K> the type of keys maintained by this map
118 * @param <V> the type of mapped values
119 *
120 * @author Josh Bloch
121 * @see HashMap
122 * @see TreeMap
123 * @see Hashtable
124 * @see SortedMap
125 * @see Collection
126 * @see Set
127 * @since 1.2
128 */
129 public interface Map<K,V> {
130 // Query Operations
131
132 /**
1216 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
1217 * @throws NullPointerException if the specified key is null and this map
1218 * does not support null keys or the value or remappingFunction is
1219 * null
1220 * @since 1.8
1221 */
1222 default V merge(K key, V value,
1223 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1224 Objects.requireNonNull(remappingFunction);
1225 Objects.requireNonNull(value);
1226 V oldValue = get(key);
1227 V newValue = (oldValue == null) ? value :
1228 remappingFunction.apply(oldValue, value);
1229 if(newValue == null) {
1230 remove(key);
1231 } else {
1232 put(key, newValue);
1233 }
1234 return newValue;
1235 }
1236 }
|
93 * only if this map contains a mapping for a key {@code k} such that
94 * {@code (key==null ? k==null : key.equals(k))}." This specification should
95 * <i>not</i> be construed to imply that invoking {@code Map.containsKey}
96 * with a non-null argument {@code key} will cause {@code key.equals(k)} to
97 * be invoked for any key {@code k}. Implementations are free to
98 * implement optimizations whereby the {@code equals} invocation is avoided,
99 * for example, by first comparing the hash codes of the two keys. (The
100 * {@link Object#hashCode()} specification guarantees that two objects with
101 * unequal hash codes cannot be equal.) More generally, implementations of
102 * the various Collections Framework interfaces are free to take advantage of
103 * the specified behavior of underlying {@link Object} methods wherever the
104 * implementor deems it appropriate.
105 *
106 * <p>Some map operations which perform recursive traversal of the map may fail
107 * with an exception for self-referential instances where the map directly or
108 * indirectly contains itself. This includes the {@code clone()},
109 * {@code equals()}, {@code hashCode()} and {@code toString()} methods.
110 * Implementations may optionally handle the self-referential scenario, however
111 * most current implementations do not do so.
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 cannot be modified. Attempts to modify them result in
122 * an {@code UnsupportedOperationException}.
123 * <li>They are truly immutable only if the contained keys and values are themselves
124 * immutable. If a key or value is mutated, the behavior of the map is unspecified.
125 * <li>They disallow null keys and values. Attempts to create them with
126 * 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 * </ul>
131 *
132 * <p>This interface is a member of the
133 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
134 * Java Collections Framework</a>.
135 *
136 * @param <K> the type of keys maintained by this map
137 * @param <V> the type of mapped values
138 *
139 * @author Josh Bloch
140 * @see HashMap
141 * @see TreeMap
142 * @see Hashtable
143 * @see SortedMap
144 * @see Collection
145 * @see Set
146 * @since 1.2
147 */
148 public interface Map<K,V> {
149 // Query Operations
150
151 /**
1235 * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
1236 * @throws NullPointerException if the specified key is null and this map
1237 * does not support null keys or the value or remappingFunction is
1238 * null
1239 * @since 1.8
1240 */
1241 default V merge(K key, V value,
1242 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1243 Objects.requireNonNull(remappingFunction);
1244 Objects.requireNonNull(value);
1245 V oldValue = get(key);
1246 V newValue = (oldValue == null) ? value :
1247 remappingFunction.apply(oldValue, value);
1248 if(newValue == null) {
1249 remove(key);
1250 } else {
1251 put(key, newValue);
1252 }
1253 return newValue;
1254 }
1255
1256 /**
1257 * Creates an immutable map containing zero mappings.
1258 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1259 *
1260 * @param <K> the map's key type
1261 * @param <V> the map's value type
1262 * @return the newly created map
1263 *
1264 * @since 9
1265 */
1266 static <K,V> Map<K,V> of() {
1267 return Collections.emptyMap();
1268 }
1269
1270 /**
1271 * Creates an immutable map containing a single mapping.
1272 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1273 *
1274 * @param <K> the map's key type
1275 * @param <V> the map's value type
1276 * @param k1 the mapping's key
1277 * @param v1 the mapping's value
1278 * @return the newly created map
1279 * @throws NullPointerException if the key or the value is null
1280 *
1281 * @since 9
1282 */
1283 static <K,V> Map<K,V> of(K k1, V v1) {
1284 return Collections.singletonMap(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1285 }
1286
1287 /**
1288 * Creates an immutable map containing two mappings.
1289 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1290 *
1291 * @param <K> the map's key type
1292 * @param <V> the map's value type
1293 * @param k1 the first mapping's key
1294 * @param v1 the first mapping's value
1295 * @param k2 the second mapping's key
1296 * @param v2 the second mapping's value
1297 * @return the newly created map
1298 * @throws IllegalArgumentException if the keys are duplicates
1299 * @throws NullPointerException if any key or value is null
1300 *
1301 * @since 9
1302 */
1303 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2) {
1304 Map<K,V> map = new HashMap<>(3);
1305 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1306 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1307 if (map.size() != 2) {
1308 throw new IllegalArgumentException("duplicate keys");
1309 }
1310 return Collections.unmodifiableMap(map);
1311 }
1312
1313 /**
1314 * Creates an immutable map containing three mappings.
1315 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1316 *
1317 * @param <K> the map's key type
1318 * @param <V> the map's value type
1319 * @param k1 the first mapping's key
1320 * @param v1 the first mapping's value
1321 * @param k2 the second mapping's key
1322 * @param v2 the second mapping's value
1323 * @param k3 the third mapping's key
1324 * @param v3 the third mapping's value
1325 * @return the newly created map
1326 * @throws IllegalArgumentException if any keys are duplicates
1327 * @throws NullPointerException if any key or value is null
1328 *
1329 * @since 9
1330 */
1331 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
1332 Map<K,V> map = new HashMap<>(5);
1333 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1334 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1335 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1336 if (map.size() != 3) {
1337 throw new IllegalArgumentException("duplicate keys");
1338 }
1339 return Collections.unmodifiableMap(map);
1340 }
1341
1342 /**
1343 * Creates an immutable map containing four mappings.
1344 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1345 *
1346 * @param <K> the map's key type
1347 * @param <V> the map's value type
1348 * @param k1 the first mapping's key
1349 * @param v1 the first mapping's value
1350 * @param k2 the second mapping's key
1351 * @param v2 the second mapping's value
1352 * @param k3 the third mapping's key
1353 * @param v3 the third mapping's value
1354 * @param k4 the fourth mapping's key
1355 * @param v4 the fourth mapping's value
1356 * @return the newly created map
1357 * @throws IllegalArgumentException if any keys are duplicates
1358 * @throws NullPointerException if any key or value is null
1359 *
1360 * @since 9
1361 */
1362 static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
1363 Map<K,V> map = new HashMap<>(6);
1364 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1365 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1366 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1367 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1368 if (map.size() != 4) {
1369 throw new IllegalArgumentException("duplicate keys");
1370 }
1371 return Collections.unmodifiableMap(map);
1372 }
1373
1374 /**
1375 * Creates an immutable map containing five mappings.
1376 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1377 *
1378 * @param <K> the map's key type
1379 * @param <V> the map's value type
1380 * @param k1 the first mapping's key
1381 * @param v1 the first mapping's value
1382 * @param k2 the second mapping's key
1383 * @param v2 the second mapping's value
1384 * @param k3 the third mapping's key
1385 * @param v3 the third mapping's value
1386 * @param k4 the fourth mapping's key
1387 * @param v4 the fourth mapping's value
1388 * @param k5 the fifth mapping's key
1389 * @param v5 the fifth mapping's value
1390 * @return the newly created map
1391 * @throws IllegalArgumentException if any keys are duplicates
1392 * @throws NullPointerException if any key or value is null
1393 *
1394 * @since 9
1395 */
1396 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) {
1397 Map<K,V> map = new HashMap<>(7);
1398 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1399 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1400 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1401 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1402 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1403 if (map.size() != 5) {
1404 throw new IllegalArgumentException("duplicate keys");
1405 }
1406 return Collections.unmodifiableMap(map);
1407 }
1408
1409 /**
1410 * Creates an immutable map containing six mappings.
1411 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1412 *
1413 * @param <K> the map's key type
1414 * @param <V> the map's value type
1415 * @param k1 the first mapping's key
1416 * @param v1 the first mapping's value
1417 * @param k2 the second mapping's key
1418 * @param v2 the second mapping's value
1419 * @param k3 the third mapping's key
1420 * @param v3 the third mapping's value
1421 * @param k4 the fourth mapping's key
1422 * @param v4 the fourth mapping's value
1423 * @param k5 the fifth mapping's key
1424 * @param v5 the fifth mapping's value
1425 * @param k6 the sixth mapping's key
1426 * @param v6 the sixth mapping's value
1427 * @return the newly created map
1428 * @throws IllegalArgumentException if any keys are duplicates
1429 * @throws NullPointerException if any key or value is null
1430 *
1431 * @since 9
1432 */
1433 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,
1434 K k6, V v6) {
1435 Map<K,V> map = new HashMap<>(9);
1436 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1437 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1438 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1439 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1440 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1441 map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
1442 if (map.size() != 6) {
1443 throw new IllegalArgumentException("duplicate keys");
1444 }
1445 return Collections.unmodifiableMap(map);
1446 }
1447
1448 /**
1449 * Creates an immutable map containing seven mappings.
1450 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1451 *
1452 * @param <K> the map's key type
1453 * @param <V> the map's value type
1454 * @param k1 the first mapping's key
1455 * @param v1 the first mapping's value
1456 * @param k2 the second mapping's key
1457 * @param v2 the second mapping's value
1458 * @param k3 the third mapping's key
1459 * @param v3 the third mapping's value
1460 * @param k4 the fourth mapping's key
1461 * @param v4 the fourth mapping's value
1462 * @param k5 the fifth mapping's key
1463 * @param v5 the fifth mapping's value
1464 * @param k6 the sixth mapping's key
1465 * @param v6 the sixth mapping's value
1466 * @param k7 the seventh mapping's key
1467 * @param v7 the seventh mapping's value
1468 * @return the newly created map
1469 * @throws IllegalArgumentException if any keys are duplicates
1470 * @throws NullPointerException if any key or value is null
1471 *
1472 * @since 9
1473 */
1474 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,
1475 K k6, V v6, K k7, V v7) {
1476 Map<K,V> map = new HashMap<>(10);
1477 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1478 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1479 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1480 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1481 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1482 map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
1483 map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
1484 if (map.size() != 7) {
1485 throw new IllegalArgumentException("duplicate keys");
1486 }
1487 return Collections.unmodifiableMap(map);
1488 }
1489
1490 /**
1491 * Creates an immutable map containing eight mappings.
1492 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1493 *
1494 * @param <K> the map's key type
1495 * @param <V> the map's value type
1496 * @param k1 the first mapping's key
1497 * @param v1 the first mapping's value
1498 * @param k2 the second mapping's key
1499 * @param v2 the second mapping's value
1500 * @param k3 the third mapping's key
1501 * @param v3 the third mapping's value
1502 * @param k4 the fourth mapping's key
1503 * @param v4 the fourth mapping's value
1504 * @param k5 the fifth mapping's key
1505 * @param v5 the fifth mapping's value
1506 * @param k6 the sixth mapping's key
1507 * @param v6 the sixth mapping's value
1508 * @param k7 the seventh mapping's key
1509 * @param v7 the seventh mapping's value
1510 * @param k8 the eighth mapping's key
1511 * @param v8 the eighth mapping's value
1512 * @return the newly created map
1513 * @throws IllegalArgumentException if any keys are duplicates
1514 * @throws NullPointerException if any key or value is null
1515 *
1516 * @since 9
1517 */
1518 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,
1519 K k6, V v6, K k7, V v7, K k8, V v8) {
1520 Map<K,V> map = new HashMap<>(11);
1521 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1522 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1523 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1524 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1525 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1526 map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
1527 map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
1528 map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
1529 if (map.size() != 8) {
1530 throw new IllegalArgumentException("duplicate keys");
1531 }
1532 return Collections.unmodifiableMap(map);
1533 }
1534
1535 /**
1536 * Creates an immutable map containing nine mappings.
1537 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1538 *
1539 * @param <K> the map's key type
1540 * @param <V> the map's value type
1541 * @param k1 the first mapping's key
1542 * @param v1 the first mapping's value
1543 * @param k2 the second mapping's key
1544 * @param v2 the second mapping's value
1545 * @param k3 the third mapping's key
1546 * @param v3 the third mapping's value
1547 * @param k4 the fourth mapping's key
1548 * @param v4 the fourth mapping's value
1549 * @param k5 the fifth mapping's key
1550 * @param v5 the fifth mapping's value
1551 * @param k6 the sixth mapping's key
1552 * @param v6 the sixth mapping's value
1553 * @param k7 the seventh mapping's key
1554 * @param v7 the seventh mapping's value
1555 * @param k8 the eighth mapping's key
1556 * @param v8 the eighth mapping's value
1557 * @param k9 the ninth mapping's key
1558 * @param v9 the ninth mapping's value
1559 * @return the newly created map
1560 * @throws IllegalArgumentException if any keys are duplicates
1561 * @throws NullPointerException if any key or value is null
1562 *
1563 * @since 9
1564 */
1565 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,
1566 K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
1567 Map<K,V> map = new HashMap<>(13);
1568 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1569 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1570 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1571 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1572 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1573 map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
1574 map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
1575 map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
1576 map.put(Objects.requireNonNull(k9), Objects.requireNonNull(v9));
1577 if (map.size() != 9) {
1578 throw new IllegalArgumentException("duplicate keys");
1579 }
1580 return Collections.unmodifiableMap(map);
1581 }
1582
1583 /**
1584 * Creates an immutable map containing ten mappings.
1585 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1586 *
1587 * @param <K> the map's key type
1588 * @param <V> the map's value type
1589 * @param k1 the first mapping's key
1590 * @param v1 the first mapping's value
1591 * @param k2 the second mapping's key
1592 * @param v2 the second mapping's value
1593 * @param k3 the third mapping's key
1594 * @param v3 the third mapping's value
1595 * @param k4 the fourth mapping's key
1596 * @param v4 the fourth mapping's value
1597 * @param k5 the fifth mapping's key
1598 * @param v5 the fifth mapping's value
1599 * @param k6 the sixth mapping's key
1600 * @param v6 the sixth mapping's value
1601 * @param k7 the seventh mapping's key
1602 * @param v7 the seventh mapping's value
1603 * @param k8 the eighth mapping's key
1604 * @param v8 the eighth mapping's value
1605 * @param k9 the ninth mapping's key
1606 * @param v9 the ninth mapping's value
1607 * @param k10 the tenth mapping's key
1608 * @param v10 the tenth mapping's value
1609 * @return the newly created map
1610 * @throws IllegalArgumentException if any keys are duplicates
1611 * @throws NullPointerException if any key or value is null
1612 *
1613 * @since 9
1614 */
1615 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,
1616 K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
1617 Map<K,V> map = new HashMap<>(14);
1618 map.put(Objects.requireNonNull(k1), Objects.requireNonNull(v1));
1619 map.put(Objects.requireNonNull(k2), Objects.requireNonNull(v2));
1620 map.put(Objects.requireNonNull(k3), Objects.requireNonNull(v3));
1621 map.put(Objects.requireNonNull(k4), Objects.requireNonNull(v4));
1622 map.put(Objects.requireNonNull(k5), Objects.requireNonNull(v5));
1623 map.put(Objects.requireNonNull(k6), Objects.requireNonNull(v6));
1624 map.put(Objects.requireNonNull(k7), Objects.requireNonNull(v7));
1625 map.put(Objects.requireNonNull(k8), Objects.requireNonNull(v8));
1626 map.put(Objects.requireNonNull(k9), Objects.requireNonNull(v9));
1627 map.put(Objects.requireNonNull(k10), Objects.requireNonNull(v10));
1628 if (map.size() != 10) {
1629 throw new IllegalArgumentException("duplicate keys");
1630 }
1631 return Collections.unmodifiableMap(map);
1632 }
1633
1634 /**
1635 * Creates an immutable map containing keys and values from the given entries.
1636 * See <a href="#immutable">Immutable Map Static Factory Methods</a> for details.
1637 *
1638 * @apiNote
1639 * It is convenient to create the map entries using the {@link Map#entry Map.entry()} method.
1640 * For example,
1641 *
1642 * <pre>{@code
1643 * import static java.util.Map.entry;
1644 *
1645 * Map<Integer,String> map = Map.ofEntries(
1646 * entry(1, "a"),
1647 * entry(2, "b"),
1648 * entry(3, "c"),
1649 * ...
1650 * entry(26, "z"));
1651 * }</pre>
1652 *
1653 * @param <K> the map's key type
1654 * @param <V> the map's value type
1655 * @param entries the keys and values with which the map is populated
1656 * @return the newly created {@code Map}
1657 * @throws IllegalArgumentException if any keys are duplicates
1658 * @throws NullPointerException if any entry, key, or value is null, or if
1659 * the {@code entries} array is null
1660 *
1661 * @see Map#entry Map.entry()
1662 * @since 9
1663 */
1664 @SafeVarargs
1665 @SuppressWarnings("varargs")
1666 static <K,V> Map<K,V> ofEntries(Entry<K,V>... entries) {
1667 Map<K,V> map = new HashMap<>(entries.length * 4 / 3 + 1); // throws NPE if entries is null
1668 for (Entry<K,V> e : entries) {
1669 Objects.requireNonNull(e);
1670 map.put(Objects.requireNonNull(e.getKey()), Objects.requireNonNull(e.getValue()));
1671 }
1672 if (map.size() != entries.length) {
1673 throw new IllegalArgumentException("duplicate elements");
1674 }
1675 return Collections.unmodifiableMap(map);
1676 }
1677
1678 /**
1679 * Creates an immutable {@link Entry} containing the given key and value.
1680 * These entries are suitable for populating {@code Map} instances using the
1681 * {@link Map#ofEntries Map.ofEntries()} method.
1682 * The key and the value must not be null. Calling {@link Entry#setValue Entry.setValue()}
1683 * on the returned {@code Entry} results in an {@code UnsupportedOperationException}
1684 * being thrown. The returned {@code Entry} is not guaranteed to be serializable.
1685 *
1686 * <p>The returned {@code Entry} is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
1687 * class; use of identity-sensitive operations (including reference equality
1688 * ({@code ==}), identity hash code, or synchronization) on any returned values
1689 * may have unpredictable results and should be avoided.
1690 *
1691 * @param <K> the key's type
1692 * @param <V> the value's type
1693 * @param k the key
1694 * @param v the value
1695 * @return the new {@code Entry}
1696 * @throws NullPointerException if the key or value is null
1697 *
1698 * @see Map#ofEntries Map.ofEntries()
1699 * @since 9
1700 */
1701 static <K,V> Entry<K,V> entry(K k, V v) {
1702 // KeyValueHolder checks for nulls
1703 return new KeyValueHolder<>(k, v);
1704 }
1705 }
|