@@ -128,28 +128,31 @@ private static <K, V> ImmutableMap<K, V> fromEntryArrayCheckingBucketOverflow(
128
128
checkEntryNotNull (key , value );
129
129
int tableIndex = Hashing .smear (key .hashCode ()) & mask ;
130
130
ImmutableMapEntry <K , V > keyBucketHead = table [tableIndex ];
131
- ImmutableMapEntry <K , V > newEntry =
131
+ ImmutableMapEntry <K , V > effectiveEntry =
132
132
checkNoConflictInKeyBucket (key , value , keyBucketHead , throwIfDuplicateKeys );
133
- if (newEntry == null ) {
133
+ if (effectiveEntry == null ) {
134
134
// prepend, not append, so the entries can be immutable
135
- newEntry =
135
+ effectiveEntry =
136
136
(keyBucketHead == null )
137
137
? makeImmutable (entry , key , value )
138
138
: new NonTerminalImmutableMapEntry <K , V >(key , value , keyBucketHead );
139
- table [tableIndex ] = newEntry ;
139
+ table [tableIndex ] = effectiveEntry ;
140
140
} else {
141
+ // We already saw this key, and the first value we saw (going backwards) is the one we are
142
+ // keeping. So we won't touch table[], but we do still want to add the existing entry that
143
+ // we found to entries[] so that we will see this key in the right place when iterating.
141
144
if (duplicates == null ) {
142
145
duplicates = new IdentityHashMap <>();
143
146
}
144
- duplicates .put (newEntry , true );
147
+ duplicates .put (effectiveEntry , true );
145
148
dupCount ++;
146
149
// Make sure we are not overwriting the original entries array, in case we later do
147
150
// buildOrThrow(). We would want an exception to include two values for the duplicate key.
148
151
if (entries == entryArray ) {
149
152
entries = entries .clone ();
150
153
}
151
154
}
152
- entries [entryIndex ] = newEntry ;
155
+ entries [entryIndex ] = effectiveEntry ;
153
156
}
154
157
if (duplicates != null ) {
155
158
// Explicit type parameters needed here to avoid a problem with nullness inference.
0 commit comments