Skip to content

Remove all nulls in a single pass#6733

Closed
timtebeek wants to merge 2 commits intomainfrom
remove-all-nulls-in-single-pass
Closed

Remove all nulls in a single pass#6733
timtebeek wants to merge 2 commits intomainfrom
remove-all-nulls-in-single-pass

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Avoids repeatedly looping over the collection for as many nulls as are present. Delegates to Collection.removeIf with

    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }

@timtebeek timtebeek added enhancement New feature or request performance labels Feb 13, 2026
Copy link
Copy Markdown
Contributor

@tkvangorder tkvangorder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Tim!

@timtebeek timtebeek marked this pull request as draft February 13, 2026 17:25
@timtebeek
Copy link
Copy Markdown
Member Author

@knutwannheden has been doing some benchmarking; looks like it's faster to keep (and document) the original, as we only sparsely have mulitple nulls that trigger repeated traversals, whereas there's a higher tax on using removeIf.

@tkvangorder
Copy link
Copy Markdown
Contributor

Interesting, so the overhead of creating the iterator + the fact that typically there are only one or two nulls means using remove(object) is quicker?

@timtebeek
Copy link
Copy Markdown
Member Author

Yep! I agree it looks odd, but that's what we're finding. I'll close this PR; we can separately add a comment to indicate so.

@timtebeek timtebeek closed this Feb 14, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants