Large collection copy (ARCH4086)
Ce contenu n’est pas encore disponible dans votre langue.
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.
Incorrect
Section titled “Incorrect”List<Item> copy = new ArrayList<>(source);return copy.stream() .filter(Item::isActive) .collect(toList());Correct
Section titled “Correct”return source.stream() .filter(Item::isActive) .collect(toList());Stream directly over the source collection instead of copying it for read-only iteration.