Copying a filtered collection with ImmutableSet.copyOf() should require visiting each element only once #7143
Labels
P2
package=collect
type=defect
Bug, not working as expected
type=enhancement
Make an existing feature better
Description
Since Google Guava 33.1.0 in 5c4f5b2, using ImmutableSet to copy a filtered collection requires iterating over and applying the predicate function, along with any side effects, to each element twice. The expected number of times is once per element.
This change message seems to indicate this behavior was understood by the author.
I believe the code should handle this case if only to avoid unnecessary executions of the filter function which may negate any potential allocation savings. If this change cannot be made, please consider replacing
size()
withisEmpty()
. For collections wheresize()
is not constant time,isEmpty()
is almost always cheaper, including for filtered collections.Example
Expected Behavior
Actual Behavior
Packages
com.google.common.collect
Platforms
No response
Checklist
The text was updated successfully, but these errors were encountered: