Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
Expand Down Expand Up @@ -1290,7 +1291,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) {
if (values instanceof Collection) {
// We can provide a hint to the builder to preallocate the correct size when the iterable is
// either a List (which likely has a fast size() implementation), or an ImmutableCollection
// (which definitely has a fast size() implementation). While Collection also has a size()
// implementation, it _may_ require iteration over the entire collection (e.g., a
// FilteredCollection), which we want to avoid.
if (values instanceof List || values instanceof ImmutableCollection) {
return uniqueIndex(
values.iterator(),
keyFunction,
Expand Down
8 changes: 7 additions & 1 deletion guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
Expand Down Expand Up @@ -1324,7 +1325,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) {
if (values instanceof Collection) {
// We can provide a hint to the builder to preallocate the correct size when the iterable is
// either a List (which likely has a fast size() implementation), or an ImmutableCollection
// (which definitely has a fast size() implementation). While Collection also has a size()
// implementation, it _may_ require iteration over the entire collection (e.g., a
// FilteredCollection), which we want to avoid.
if (values instanceof List || values instanceof ImmutableCollection) {
return uniqueIndex(
values.iterator(),
keyFunction,
Expand Down
Loading