Prefer empty collection constant (ARCH4088)
Esta página aún no está disponible en tu idioma.
Key java.performance.prefer-empty-collection-constant · Severity info · Category performance
Returning a fresh empty collection on every call is a needless allocation. Collections.emptyList() or List.of() returns an immutable, shared instance and avoids the allocation.
Incorrect
Section titled “Incorrect”public List<String> tags() { if (raw == null) { return new ArrayList<>(); } return parse(raw);}Correct
Section titled “Correct”public List<String> tags() { if (raw == null) { return List.of(); } return parse(raw);}Return List.of() or Collections.emptyList() instead of new ArrayList<>() for empty results.