Skip to content

Commit

Permalink
collect: Presize the ImmutableMap builder when uniqueIndex is r…
Browse files Browse the repository at this point in the history
…un on a `Collection`

RELNOTES=`collect`: Presize the `ImmutableMap` builder when `uniqueIndex` is run on a `Collection`
PiperOrigin-RevId: 469418201
  • Loading branch information
nearp authored and Google Java Core Libraries committed Aug 23, 2022
1 parent 4312d94 commit 101dc3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,12 @@ public static <K, V> ImmutableMap<K, V> toMap(
@CanIgnoreReturnValue
public static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterable<V> values, Function<? super V, K> keyFunction) {
// TODO(lowasser): consider presizing the builder if values is a Collection
if (values instanceof Collection) {
return uniqueIndex(
values.iterator(),
keyFunction,
ImmutableMap.builderWithExpectedSize(((Collection<?>) values).size()));
}
return uniqueIndex(values.iterator(), keyFunction);
}

Expand Down Expand Up @@ -1318,8 +1323,12 @@ public static <K, V> ImmutableMap<K, V> uniqueIndex(
@CanIgnoreReturnValue
public static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterator<V> values, Function<? super V, K> keyFunction) {
return uniqueIndex(values, keyFunction, ImmutableMap.builder());
}

private static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterator<V> values, Function<? super V, K> keyFunction, ImmutableMap.Builder<K, V> builder) {
checkNotNull(keyFunction);
ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
while (values.hasNext()) {
V value = values.next();
builder.put(keyFunction.apply(value), value);
Expand Down
13 changes: 11 additions & 2 deletions guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,12 @@ public static <K, V> ImmutableMap<K, V> toMap(
@CanIgnoreReturnValue
public static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterable<V> values, Function<? super V, K> keyFunction) {
// TODO(lowasser): consider presizing the builder if values is a Collection
if (values instanceof Collection) {
return uniqueIndex(
values.iterator(),
keyFunction,
ImmutableMap.builderWithExpectedSize(((Collection<?>) values).size()));
}
return uniqueIndex(values.iterator(), keyFunction);
}

Expand Down Expand Up @@ -1394,8 +1399,12 @@ public static <K, V> ImmutableMap<K, V> uniqueIndex(
@CanIgnoreReturnValue
public static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterator<V> values, Function<? super V, K> keyFunction) {
return uniqueIndex(values, keyFunction, ImmutableMap.builder());
}

private static <K, V> ImmutableMap<K, V> uniqueIndex(
Iterator<V> values, Function<? super V, K> keyFunction, ImmutableMap.Builder<K, V> builder) {
checkNotNull(keyFunction);
ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
while (values.hasNext()) {
V value = values.next();
builder.put(keyFunction.apply(value), value);
Expand Down

0 comments on commit 101dc3e

Please sign in to comment.