Avoid redundant filtering in FilteredMap.size()

Since keySet() already applies the filter, size() evaluated the
predicate twice for every accepted key.

This commit uses delegate.keySet() instead, avoiding the second
evaluation as well as a FilteredSet and FilteredIterator allocation.

Closes gh-37256

Signed-off-by: Hyunwoo Jung <hyunwoojung@kakao.com>
This commit is contained in:
Hyunwoo Jung
2026-09-14 14:31:42 +02:00
committed by GitHub
parent bcd78a2ccc
commit 9d1156f1fd
@@ -56,7 +56,7 @@ final class FilteredMap<K, V extends @Nullable Object> extends AbstractMap<K, V>
@Override
public int size() {
int size = 0;
for (K k : keySet()) {
for (K k : this.delegate.keySet()) {
if (this.filter.test(k)) {
size++;
}