From 1916f618fa93eb3d6c774a1995aee4adacf8e93e Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 8 Nov 2023 13:19:01 -0800 Subject: [PATCH] Document and test how `ImmutableMap.toImmutableMap` behaves when the `mergeFunction` returns `null`. (The test is Google-internal for now because we're in the process of reshuffling our `Collector` tests to make them run under Android as part of https://github.com/google/guava/issues/6567. Also, we skip the test under GWT+J2CL because of a bug in their implementation of `Collectors.toMap`.) This addresses the main part of https://github.com/google/guava/issues/6824, but I'm keeping the bug open as a prompt to recognize our nullness annotations in the future. RELNOTES=n/a PiperOrigin-RevId: 580635720 --- guava/src/com/google/common/collect/ImmutableMap.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/guava/src/com/google/common/collect/ImmutableMap.java b/guava/src/com/google/common/collect/ImmutableMap.java index e2e6cd5ae03f..b3a6b1943863 100644 --- a/guava/src/com/google/common/collect/ImmutableMap.java +++ b/guava/src/com/google/common/collect/ImmutableMap.java @@ -96,8 +96,11 @@ public abstract class ImmutableMap implements Map, Serializable { * and values are the result of applying the provided mapping functions to the input elements. * *

If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the - * values are merged using the specified merging function. Entries will appear in the encounter - * order of the first occurrence of the key. + * values are merged using the specified merging function. If the merging function returns {@code + * null}, then the collector removes the value that has been computed for the key thus far (though + * future occurrences of the key would reinsert it). + * + *

Entries will appear in the encounter order of the first occurrence of the key. * * @since 21.0 */