Skip to content

Commit 89a2e99

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Update javadoc of ImmutableMap.Builder to reflect buildKeepingLast() changes.
It is no longer necessarily the case that a `put` of a duplicate key will lead to an exception. Although `buildKeepingLast()` will usually perform better than the equivalent `LinkedHashMap` + `ImmutableMap.copyOf`, the builder does retain all key/value pairs that were put. So in some cases it may still be preferable to use `LinkedHashMap`. RELNOTES=n/a PiperOrigin-RevId: 418040612
1 parent 448e326 commit 89a2e99

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

android/guava/src/com/google/common/collect/ImmutableMap.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,9 @@ private void ensureCapacity(int minCapacity) {
420420
}
421421

422422
/**
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.
425426
*/
426427
@CanIgnoreReturnValue
427428
public Builder<K, V> put(K key, V value) {
@@ -434,8 +435,9 @@ public Builder<K, V> put(K key, V value) {
434435
}
435436

436437
/**
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.
439441
*
440442
* @since 11.0
441443
*/
@@ -445,8 +447,9 @@ public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
445447
}
446448

447449
/**
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.
450453
*
451454
* @throws NullPointerException if any key or value in {@code map} is null
452455
*/
@@ -456,8 +459,9 @@ public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
456459
}
457460

458461
/**
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.
461465
*
462466
* @throws NullPointerException if any key, value, or entry is null
463467
* @since 19.0
@@ -582,6 +586,12 @@ public ImmutableMap<K, V> buildOrThrow() {
582586
* entries are sorted by value. If a key was added more than once, it appears in iteration order
583587
* based on the first time it was added, again unless {@link #orderEntriesByValue} was called.
584588
*
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+
*
585595
* @since NEXT
586596
*/
587597
public ImmutableMap<K, V> buildKeepingLast() {

guava/src/com/google/common/collect/ImmutableMap.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,9 @@ private void ensureCapacity(int minCapacity) {
439439
}
440440

441441
/**
442-
* Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
443-
* and will cause {@link #build} to fail.
442+
* Associates {@code key} with {@code value} in the built map. If the same key is put more than
443+
* once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last
444+
* value put for that key.
444445
*/
445446
@CanIgnoreReturnValue
446447
public Builder<K, V> put(K key, V value) {
@@ -452,8 +453,9 @@ public Builder<K, V> put(K key, V value) {
452453
}
453454

454455
/**
455-
* Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are
456-
* not allowed, and will cause {@link #build} to fail.
456+
* Adds the given {@code entry} to the map, making it immutable if necessary. If the same key is
457+
* put more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will
458+
* keep the last value put for that key.
457459
*
458460
* @since 11.0
459461
*/
@@ -463,8 +465,9 @@ public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
463465
}
464466

465467
/**
466-
* Associates all of the given map's keys and values in the built map. Duplicate keys are not
467-
* allowed, and will cause {@link #build} to fail.
468+
* Associates all of the given map's keys and values in the built map. If the same key is put
469+
* more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep
470+
* the last value put for that key.
468471
*
469472
* @throws NullPointerException if any key or value in {@code map} is null
470473
*/
@@ -474,8 +477,9 @@ public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
474477
}
475478

476479
/**
477-
* Adds all of the given entries to the built map. Duplicate keys are not allowed, and will
478-
* cause {@link #build} to fail.
480+
* Adds all of the given entries to the built map. If the same key is put more than once, {@link
481+
* #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last value put for
482+
* that key.
479483
*
480484
* @throws NullPointerException if any key, value, or entry is null
481485
* @since 19.0
@@ -604,6 +608,12 @@ public ImmutableMap<K, V> buildOrThrow() {
604608
* entries are sorted by value. If a key was added more than once, it appears in iteration order
605609
* based on the first time it was added, again unless {@link #orderEntriesByValue} was called.
606610
*
611+
* <p>In the current implementation, all values associated with a given key are stored in the
612+
* {@code Builder} object, even though only one of them will be used in the built map. If there
613+
* can be many repeated keys, it may be more space-efficient to use a {@link
614+
* java.util.LinkedHashMap LinkedHashMap} and {@link ImmutableMap#copyOf} rather than {@code
615+
* ImmutableMap.Builder}.
616+
*
607617
* @since NEXT
608618
*/
609619
public ImmutableMap<K, V> buildKeepingLast() {

0 commit comments

Comments
 (0)