İçeriğe geç

Boş koleksiyon sabiti tercih et (ARCH4088)

Anahtar java.performance.prefer-empty-collection-constant · Önem info · Kategori performance

Her çağrıda taze bir boş koleksiyon döndürmek gereksiz bir tahsisattır. Collections.emptyList() veya List.of() değişmez, paylaşılan bir örnek döndürerek tahsisattan kaçınır.

public List<String> tags() {
if (raw == null) {
return new ArrayList<>();
}
return parse(raw);
}
public List<String> tags() {
if (raw == null) {
return List.of();
}
return parse(raw);
}

Boş sonuçlar için new ArrayList<>() yerine List.of() veya Collections.emptyList() döndürün.