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 } |