{"record":{"id":"8458ee3dd60d2609","repo":"TheAlgorithms/Java","slug":"initialcapacity-1","errorCode":null,"errorMessage":"initialCapacity < 1","messagePattern":"initialCapacity < 1","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java","lineNumber":77,"sourceCode":"     * </ul>\n     * If you prefer value-based semantics, replace with HashMap<E,Integer> and\n     * respect the warnings in the class Javadoc.\n     */\n    private final IdentityHashMap<E, Integer> index;\n\n    private static final int DEFAULT_INITIAL_CAPACITY = 11;\n\n    public IndexedPriorityQueue() {\n        this(DEFAULT_INITIAL_CAPACITY, null);\n    }\n\n    public IndexedPriorityQueue(Comparator<? super E> cmp) {\n        this(DEFAULT_INITIAL_CAPACITY, cmp);\n    }\n\n    public IndexedPriorityQueue(int initialCapacity, Comparator<? super E> cmp) {\n        if (initialCapacity < 1) {\n            throw new IllegalArgumentException(\"initialCapacity < 1\");\n        }\n        this.heap = new Object[initialCapacity];\n        this.cmp = cmp;\n        this.index = new IdentityHashMap<>();\n    }\n\n    /** Returns current number of elements. */\n    public int size() {\n        return size;\n    }\n\n    /** Returns {@code true} if empty. */\n    public boolean isEmpty() {\n        return size == 0;\n    }\n\n    /**\n     * Returns the minimum element without removing it, or {@code null} if empty.","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java#L59-L95","documentation":"Thrown by the IndexedPriorityQueue(int initialCapacity, Comparator) constructor when initialCapacity is less than 1. The constructor immediately allocates `new Object[initialCapacity]`, so a zero or negative value would yield an unusable or malformed backing array. The no-arg and Comparator-only constructors default to 11 to avoid this.","triggerScenarios":"Passing 0 explicitly; passing a negative number; computing capacity as `collection.size()` on an empty collection; wiring a config property that defaults to 0.","commonSituations":"Capacity read from a properties/YAML file with no default; sizing math with an off-by-one (`size - 1`); DI/bean configuration supplying an unset int field.","solutions":["Pass a value >= 1, or use the no-arg / Comparator-only constructor to get the default capacity of 11.","Clamp externally sourced capacity: `Math.max(1, configuredCapacity)` before constructing.","Validate configuration at load time and fail fast with a clear config error rather than at PQ construction."],"exampleFix":"// before\nnew IndexedPriorityQueue<>(config.getCapacity(), cmp);\n\n// after\nint cap = Math.max(1, config.getCapacity());\nnew IndexedPriorityQueue<>(cap, cmp);","handlingStrategy":"validation","validationCode":"int cap = Math.max(1, configuredCapacity);\nnew IndexedPriorityQueue<>(cap, cmp);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default capacity reads from config to 11 (or another positive value), never 0.","Prefer the no-arg or Comparator-only constructor when default capacity is acceptable.","Validate capacity at configuration load time and fail fast with a clear message."],"tags":["heap","priority-queue","constructor","capacity","validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}