Skip to content

Commit

Permalink
Remove unneeded copies done by CollectionUtils. (#10474)
Browse files Browse the repository at this point in the history
* Remove unneeded copies done by CollectionUtils.

* Formatting.
  • Loading branch information
asvitkine authored May 21, 2022
1 parent 4a08a50 commit 6280d03
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand All @@ -34,7 +35,7 @@ public static <T> int countMatches(final Collection<T> collection, final Predica
checkNotNull(collection);
checkNotNull(predicate);

return (int) ImmutableList.copyOf(collection).stream().filter(predicate).count();
return (int) collection.stream().filter(predicate).count();
}

/**
Expand All @@ -49,7 +50,7 @@ public static <T> List<T> getMatches(
checkNotNull(collection);
checkNotNull(predicate);

return ImmutableList.copyOf(collection).stream().filter(predicate).collect(Collectors.toList());
return collection.stream().filter(predicate).collect(Collectors.toList());
}

/**
Expand All @@ -68,22 +69,21 @@ public static <T> List<T> getNMatches(
checkArgument(max >= 0, "max must not be negative");
checkNotNull(predicate);

return ImmutableList.copyOf(collection).stream()
.filter(predicate)
.limit(max)
.collect(Collectors.toList());
return collection.stream().filter(predicate).limit(max).collect(Collectors.toList());
}

/** return a such that a exists in c1 and a exists in c2. always returns a new collection. */
public static <T> List<T> intersection(
final Collection<T> collection1, final Collection<T> collection2) {
if (collection1 == null || collection2 == null) {
if (collection1 == null
|| collection2 == null
|| collection1.isEmpty()
|| collection2.isEmpty()) {
return new ArrayList<>();
}
final Collection<T> c1 = ImmutableSet.copyOf(collection1);
final Collection<T> c2 = ImmutableSet.copyOf(collection2);

return c1.stream().filter(c2::contains).collect(Collectors.toList());
final Collection<T> c2 =
(collection2 instanceof Set) ? collection2 : ImmutableSet.copyOf(collection2);
return collection1.stream().distinct().filter(c2::contains).collect(Collectors.toList());
}

/** Returns a such that a exists in c1 but not in c2. Always returns a new collection. */
Expand All @@ -96,10 +96,9 @@ public static <T> List<T> difference(
return new ArrayList<>(collection1);
}

final Collection<T> c1 = ImmutableSet.copyOf(collection1);
final Collection<T> c2 = ImmutableSet.copyOf(collection2);

return ImmutableList.copyOf(c1).stream().filter(not(c2::contains)).collect(Collectors.toList());
final Collection<T> c2 =
(collection2 instanceof Set) ? collection2 : ImmutableSet.copyOf(collection2);
return collection1.stream().distinct().filter(not(c2::contains)).collect(Collectors.toList());
}

/**
Expand Down

0 comments on commit 6280d03

Please sign in to comment.