@@ -420,8 +420,9 @@ private void ensureCapacity(int minCapacity) {
420
420
}
421
421
422
422
/**
423
- * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
424
- * and will cause {@link #build} to fail.
423
+ * Associates {@code key} with {@code value} in the built map. If the same key is put more than
424
+ * once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last
425
+ * value put for that key.
425
426
*/
426
427
@ CanIgnoreReturnValue
427
428
public Builder <K , V > put (K key , V value ) {
@@ -434,8 +435,9 @@ public Builder<K, V> put(K key, V value) {
434
435
}
435
436
436
437
/**
437
- * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are
438
- * not allowed, and will cause {@link #build} to fail.
438
+ * Adds the given {@code entry} to the map, making it immutable if necessary. If the same key is
439
+ * put more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will
440
+ * keep the last value put for that key.
439
441
*
440
442
* @since 11.0
441
443
*/
@@ -445,8 +447,9 @@ public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
445
447
}
446
448
447
449
/**
448
- * Associates all of the given map's keys and values in the built map. Duplicate keys are not
449
- * allowed, and will cause {@link #build} to fail.
450
+ * Associates all of the given map's keys and values in the built map. If the same key is put
451
+ * more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep
452
+ * the last value put for that key.
450
453
*
451
454
* @throws NullPointerException if any key or value in {@code map} is null
452
455
*/
@@ -456,8 +459,9 @@ public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
456
459
}
457
460
458
461
/**
459
- * Adds all of the given entries to the built map. Duplicate keys are not allowed, and will
460
- * cause {@link #build} to fail.
462
+ * Adds all of the given entries to the built map. If the same key is put more than once, {@link
463
+ * #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last value put for
464
+ * that key.
461
465
*
462
466
* @throws NullPointerException if any key, value, or entry is null
463
467
* @since 19.0
@@ -582,6 +586,12 @@ public ImmutableMap<K, V> buildOrThrow() {
582
586
* entries are sorted by value. If a key was added more than once, it appears in iteration order
583
587
* based on the first time it was added, again unless {@link #orderEntriesByValue} was called.
584
588
*
589
+ * <p>In the current implementation, all values associated with a given key are stored in the
590
+ * {@code Builder} object, even though only one of them will be used in the built map. If there
591
+ * can be many repeated keys, it may be more space-efficient to use a {@link
592
+ * java.util.LinkedHashMap LinkedHashMap} and {@link ImmutableMap#copyOf} rather than {@code
593
+ * ImmutableMap.Builder}.
594
+ *
585
595
* @since NEXT
586
596
*/
587
597
public ImmutableMap <K , V > buildKeepingLast () {
0 commit comments