Why this matters
Hash-based collections (e.g., `HashMap`, `HashSet`) should be initialized with proper capacity settings to avoid unnecessary resizing and performance overhead.
Ensure that `HashMap` and `HashSet` are initialized with appropriate capacity settings to optimize performance.
Hash-based collections (e.g., `HashMap`, `HashSet`) should be initialized with proper capacity settings to avoid unnecessary resizing and performance overhead.
Side-by-side examples engineers can pattern-match during review.
private static final int KNOWN_CAPACITY = 1_000_000;
public static Map<String, Integer> buildAMap() {
return new HashMap<>(KNOWN_CAPACITY); // Noncompliant
}
public static Set<String> buildASet() {
return new HashSet<>(KNOWN_CAPACITY); // Noncompliant
}private static final int KNOWN_CAPACITY = 1_000_000;
public static Map<String, Integer> buildABetterMap() {
return HashMap.newHashMap(KNOWN_CAPACITY);
}
public static Set<String> buildABetterSet() {
return HashSet.newHashSet(KNOWN_CAPACITY);
}
public static Set<String> buildABetterSet(float customLoadFactor) {
return new HashSet<>(KNOWN_CAPACITY, customLoadFactor);
}private static final int KNOWN_CAPACITY = 1_000_000;
public static Map<String, Integer> buildAMap() {
return new HashMap<>(KNOWN_CAPACITY); // Noncompliant
}
public static Set<String> buildASet() {
return new HashSet<>(KNOWN_CAPACITY); // Noncompliant
}private static final int KNOWN_CAPACITY = 1_000_000;
public static Map<String, Integer> buildABetterMap() {
return HashMap.newHashMap(KNOWN_CAPACITY);
}
public static Set<String> buildABetterSet() {
return HashSet.newHashSet(KNOWN_CAPACITY);
}
public static Set<String> buildABetterSet(float customLoadFactor) {
return new HashSet<>(KNOWN_CAPACITY, customLoadFactor);
}From the same buckets as this rule.
All static JS/CSS/font/image files MUST use content-hashed filenames (e.g., app.9c1a7b.js) and be served with "Cache-Control: public, max-age=31536000, immutable". HTML and other non-fingerprinted documents MUST be served with "Cache-Control: no-cache" (or equivalent) to enable conditional revalidation.