49
49
* designed for use only in the rare cases wherein reference-equality
50
50
* semantics are required.</b>
51
51
*
52
+ * <p>The view collections of this map also have reference-equality semantics
53
+ * for their elements. See the {@link keySet() keySet}, {@link values() values},
54
+ * and {@link entrySet() entrySet} methods for further information.
55
+ *
52
56
* <p>A typical use of this class is <i>topology-preserving object graph
53
57
* transformations</i>, such as serialization or deep-copying. To perform such
54
58
* a transformation, a program must maintain a "node table" that keeps track
@@ -346,7 +350,8 @@ public V get(Object key) {
346
350
347
351
/**
348
352
* Tests whether the specified object reference is a key in this identity
349
- * hash map.
353
+ * hash map. Returns {@code true} if and only if this map contains a mapping
354
+ * with key {@code k} such that {@code (key == k)}.
350
355
*
351
356
* @param key possible key
352
357
* @return {@code true} if the specified object reference is a key
@@ -370,7 +375,8 @@ public boolean containsKey(Object key) {
370
375
371
376
/**
372
377
* Tests whether the specified object reference is a value in this identity
373
- * hash map.
378
+ * hash map. Returns {@code true} if and only if this map contains a mapping
379
+ * with value {@code v} such that {@code (value == v)}.
374
380
*
375
381
* @param value value whose presence in this map is to be tested
376
382
* @return {@code true} if this map maps one or more keys to the
@@ -411,8 +417,9 @@ private boolean containsMapping(Object key, Object value) {
411
417
412
418
/**
413
419
* Associates the specified value with the specified key in this identity
414
- * hash map. If the map previously contained a mapping for the key, the
415
- * old value is replaced.
420
+ * hash map. If this map already {@link containsKey(Object) contains}
421
+ * a mapping for the key, the old value is replaced, otherwise, a new mapping
422
+ * is inserted into this map.
416
423
*
417
424
* @param key the key with which the specified value is to be associated
418
425
* @param value the value to be associated with the specified key
@@ -497,8 +504,10 @@ private boolean resize(int newCapacity) {
497
504
498
505
/**
499
506
* Copies all of the mappings from the specified map to this map.
500
- * These mappings will replace any mappings that this map had for
501
- * any of the keys currently in the specified map.
507
+ * For each mapping in the specified map, if this map already
508
+ * {@link containsKey(Object) contains} a mapping for the key,
509
+ * its value is replaced with the value from the specified map;
510
+ * otherwise, a new mapping is inserted into this map.
502
511
*
503
512
* @param m mappings to be stored in this map
504
513
* @throws NullPointerException if the specified map is null
@@ -516,6 +525,8 @@ public void putAll(Map<? extends K, ? extends V> m) {
516
525
517
526
/**
518
527
* Removes the mapping for this key from this map if present.
528
+ * The mapping is removed if and only if the mapping has a key
529
+ * {@code k} such that (key == k).
519
530
*
520
531
* @param key key whose mapping is to be removed from the map
521
532
* @return the previous value associated with {@code key}, or
@@ -632,7 +643,9 @@ public void clear() {
632
643
* {@code true} if the given object is also a map and the two maps
633
644
* represent identical object-reference mappings. More formally, this
634
645
* map is equal to another map {@code m} if and only if
635
- * {@code this.entrySet().equals(m.entrySet())}.
646
+ * {@code this.entrySet().equals(m.entrySet())}. See the
647
+ * {@link entrySet() entrySet} method for the specification of equality
648
+ * of this map's entries.
636
649
*
637
650
* <p><b>Owing to the reference-equality-based semantics of this map it is
638
651
* possible that the symmetry and transitivity requirements of the
@@ -667,8 +680,11 @@ public boolean equals(Object o) {
667
680
668
681
/**
669
682
* Returns the hash code value for this map. The hash code of a map is
670
- * defined to be the sum of the hash codes of each entry in the map's
671
- * {@code entrySet()} view. This ensures that {@code m1.equals(m2)}
683
+ * defined to be the sum of the hash codes of each entry of this map.
684
+ * See the {@link entrySet() entrySet} method for a specification of the
685
+ * hash code of this map's entries.
686
+ *
687
+ * <p>This specification ensures that {@code m1.equals(m2)}
672
688
* implies that {@code m1.hashCode()==m2.hashCode()} for any two
673
689
* {@code IdentityHashMap} instances {@code m1} and {@code m2}, as
674
690
* required by the general contract of {@link Object#hashCode}.
@@ -1162,7 +1178,9 @@ public Spliterator<V> spliterator() {
1162
1178
* e.getValue()==o.getValue()}. To accommodate these equals
1163
1179
* semantics, the {@code hashCode} method returns
1164
1180
* {@code System.identityHashCode(e.getKey()) ^
1165
- * System.identityHashCode(e.getValue())}.
1181
+ * System.identityHashCode(e.getValue())}. (While the keys and values
1182
+ * are compared using reference equality, the {@code Map.Entry}
1183
+ * objects themselves are not.)
1166
1184
*
1167
1185
* <p><b>Owing to the reference-equality-based semantics of the
1168
1186
* {@code Map.Entry} instances in the set returned by this method,
@@ -1382,6 +1400,50 @@ public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1382
1400
}
1383
1401
}
1384
1402
1403
+ /**
1404
+ * {@inheritDoc}
1405
+ *
1406
+ * <p>More formally, if this map contains a mapping from a key
1407
+ * {@code k} to a value {@code v} such that {@code (key == k)}
1408
+ * and {@code (value == v)}, then this method removes the mapping
1409
+ * for this key and returns {@code true}; otherwise it returns
1410
+ * {@code false}.
1411
+ */
1412
+ @ Override
1413
+ public boolean remove (Object key , Object value ) {
1414
+ return removeMapping (key , value );
1415
+ }
1416
+
1417
+ /**
1418
+ * {@inheritDoc}
1419
+ *
1420
+ * <p>More formally, if this map contains a mapping from a key
1421
+ * {@code k} to a value {@code v} such that {@code (key == k)}
1422
+ * and {@code (oldValue == v)}, then this method associates
1423
+ * {@code k} with {@code newValue} and returns {@code true};
1424
+ * otherwise it returns {@code false}.
1425
+ */
1426
+ @ Override
1427
+ public boolean replace (K key , V oldValue , V newValue ) {
1428
+ Object k = maskNull (key );
1429
+ Object [] tab = table ;
1430
+ int len = tab .length ;
1431
+ int i = hash (k , len );
1432
+
1433
+ while (true ) {
1434
+ Object item = tab [i ];
1435
+ if (item == k ) {
1436
+ if (tab [i + 1 ] != oldValue )
1437
+ return false ;
1438
+ tab [i + 1 ] = newValue ;
1439
+ return true ;
1440
+ }
1441
+ if (item == null )
1442
+ return false ;
1443
+ i = nextKeyIndex (i , len );
1444
+ }
1445
+ }
1446
+
1385
1447
/**
1386
1448
* Similar form as array-based Spliterators, but skips blank elements,
1387
1449
* and guestimates size as decreasing by half per split.
0 commit comments