Skip to content

[Pattern] Collection bulk operations instead of mutation loops #176

Description

@brunoborges

Category

collections

Slug

collection-bulk-operations

Title

Collection bulk operations instead of mutation loops

Difficulty

beginner

Since JDK

8

Summary

Use removeIf(), replaceAll(), and List.sort() for direct collection transformations.

Old code label

Iterator mutation loop

Old code

for (Iterator<Order> it = orders.iterator(); it.hasNext();) {
    if (it.next().cancelled()) {
        it.remove();
    }
}

Modern code label

Java 8+

Modern code

orders.removeIf(Order::cancelled);

Explanation

Modern collection interfaces expose common mutations directly. These methods replace control-flow-heavy loops with operations that state the intended transformation.

Why the modern way wins

👁 Clear intent — The operation describes what changes, not how to iterate.
🐛 Safer mutation — Avoids manual iterator-removal rules.
⚡ Less code — Common transformations become a single expression.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    slugNew or updated pattern snippet (category/slug.json)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions