Skip to content

Commit b8fd0e6

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Use ImmutableMap.Builder.buildKeepingLast() where possible.
RELNOTES=n/a PiperOrigin-RevId: 416847993
1 parent 189d668 commit b8fd0e6

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

android/guava/src/com/google/common/cache/LocalCache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.common.cache.CacheBuilder.OneWeigher;
3737
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
3838
import com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException;
39+
import com.google.common.cache.LocalCache.AbstractCacheSet;
3940
import com.google.common.collect.AbstractSequentialIterator;
4041
import com.google.common.collect.ImmutableMap;
4142
import com.google.common.collect.ImmutableSet;
@@ -3880,7 +3881,7 @@ ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
38803881
int hits = 0;
38813882
int misses = 0;
38823883

3883-
Map<K, V> result = Maps.newLinkedHashMap();
3884+
ImmutableMap.Builder<K, V> result = ImmutableMap.builder();
38843885
for (Object key : keys) {
38853886
V value = get(key);
38863887
if (value == null) {
@@ -3895,7 +3896,7 @@ ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
38953896
}
38963897
globalStatsCounter.recordHits(hits);
38973898
globalStatsCounter.recordMisses(misses);
3898-
return ImmutableMap.copyOf(result);
3899+
return result.buildKeepingLast();
38993900
}
39003901

39013902
ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,13 +1220,13 @@ public static <K, V> ImmutableMap<K, V> toMap(
12201220
public static <K, V> ImmutableMap<K, V> toMap(
12211221
Iterator<K> keys, Function<? super K, V> valueFunction) {
12221222
checkNotNull(valueFunction);
1223-
// Using LHM instead of a builder so as not to fail on duplicate keys
1224-
Map<K, V> builder = newLinkedHashMap();
1223+
ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
12251224
while (keys.hasNext()) {
12261225
K key = keys.next();
12271226
builder.put(key, valueFunction.apply(key));
12281227
}
1229-
return ImmutableMap.copyOf(builder);
1228+
// Using buildKeepingLast() so as not to fail on duplicate keys
1229+
return builder.buildKeepingLast();
12301230
}
12311231

12321232
/**

guava/src/com/google/common/cache/LocalCache.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.google.common.cache.CacheBuilder.OneWeigher;
3636
import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
3737
import com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException;
38-
import com.google.common.cache.LocalCache.AbstractCacheSet;
3938
import com.google.common.collect.AbstractSequentialIterator;
4039
import com.google.common.collect.ImmutableMap;
4140
import com.google.common.collect.ImmutableSet;
@@ -3993,7 +3992,7 @@ ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
39933992
int hits = 0;
39943993
int misses = 0;
39953994

3996-
Map<K, V> result = Maps.newLinkedHashMap();
3995+
ImmutableMap.Builder<K, V> result = ImmutableMap.builder();
39973996
for (Object key : keys) {
39983997
V value = get(key);
39993998
if (value == null) {
@@ -4008,7 +4007,7 @@ ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
40084007
}
40094008
globalStatsCounter.recordHits(hits);
40104009
globalStatsCounter.recordMisses(misses);
4011-
return ImmutableMap.copyOf(result);
4010+
return result.buildKeepingLast();
40124011
}
40134012

40144013
ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,13 @@ public static <K, V> ImmutableMap<K, V> toMap(
12961296
public static <K, V> ImmutableMap<K, V> toMap(
12971297
Iterator<K> keys, Function<? super K, V> valueFunction) {
12981298
checkNotNull(valueFunction);
1299-
// Using LHM instead of a builder so as not to fail on duplicate keys
1300-
Map<K, V> builder = newLinkedHashMap();
1299+
ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
13011300
while (keys.hasNext()) {
13021301
K key = keys.next();
13031302
builder.put(key, valueFunction.apply(key));
13041303
}
1305-
return ImmutableMap.copyOf(builder);
1304+
// Using buildKeepingLast() so as not to fail on duplicate keys
1305+
return builder.buildKeepingLast();
13061306
}
13071307

13081308
/**

0 commit comments

Comments
 (0)