Skip to content

Large collection copy (ARCH4086)

Key java.performance.large-collection-copy · Severity info · Category performance

Copying an entire collection into a new list just to iterate it creates a needless allocation and an O(n) copy. You can stream directly over the source collection.

List<Item> copy = new ArrayList<>(source);
return copy.stream()
.filter(Item::isActive)
.collect(toList());
return source.stream()
.filter(Item::isActive)
.collect(toList());

Stream directly over the source collection instead of copying it for read-only iteration.