In-Loop Collection Building¶
Rule details
| Rule ID | in-loop-collection-building |
| Severity | WARNING — likely performance problem |
| Confidence | MEDIUM — likely correct, some context-dependent |
| Category | Loop amplifiers |
| Complexity | O(n·m) → O(n+m) |
Description¶
Detects patterns that repeatedly copy or rebuild collections inside loops. Calling addAll(), putAll(), or concat() inside a loop copies elements on every iteration, turning an O(n) loop into O(n·m) due to repeated array/list copying.
Typical¶
After¶
Suggestion¶
Accumulate into a single collection, then call addAll() once after the loop
Related Rules¶
- Nested Lookup — another pattern where loop bodies cause quadratic behavior