{"record":{"id":"3c480f27d1c0913d","repo":"TheAlgorithms/Java","slug":"heap-is-empty","errorCode":null,"errorMessage":"Heap is empty","messagePattern":"Heap is empty","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java","lineNumber":69,"sourceCode":"    }\n\n    /**\n     * Checks if the heap is empty.\n     *\n     * @return true if the heap is empty, false otherwise\n     */\n    public boolean isEmpty() {\n        return this.size() == 0;\n    }\n\n    /**\n     * Removes and returns the maximum item from the heap.\n     *\n     * @return the maximum item\n     */\n    public T remove() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Heap is empty\");\n        }\n        this.swap(0, this.size() - 1);\n        T rv = this.data.remove(this.size() - 1);\n        map.remove(rv);\n        downHeapify(0);\n        return rv;\n    }\n\n    /**\n     * Restores the heap property by moving the item at the given index downwards.\n     *\n     * @param pi the index of the current item\n     */\n    private void downHeapify(int pi) {\n        int lci = 2 * pi + 1;\n        int rci = 2 * pi + 2;\n        int mini = pi;\n        if (lci < this.size() && isLarger(this.data.get(lci), this.data.get(mini)) > 0) {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java#L51-L87","documentation":"Thrown by GenericHeap.remove() when the heap has no elements. remove() swaps the root with the last element, drops it, and re-heapifies; with size 0 there is no root to return and swap(0, -1) would be invalid. IllegalStateException signals an operation invoked in a wrong state rather than a bad argument.","triggerScenarios":"Calling remove() on a freshly constructed heap with no add() calls; calling remove() more times than elements were added; a drain loop with no bound check.","commonSituations":"Priority-queue drain loops (`while(true) heap.remove()`); event loops where events are exhausted; test fixtures that forget to populate the heap.","solutions":["Guard with `if (!heap.isEmpty()) heap.remove();`.","Bound the drain loop by `for (int i = 0; i < heap.size(); ) heap.remove();` or capture size first.","Wrap remove() in a helper that returns Optional<T> and returns empty when the heap is empty."],"exampleFix":"// before\nT top = heap.remove();\n\n// after\nT top = heap.isEmpty() ? null : heap.remove();","handlingStrategy":"validation","validationCode":"if (!heap.isEmpty()) {\n    T top = heap.remove();\n}","typeGuard":null,"tryCatchPattern":"try {\n    T top = heap.remove();\n} catch (IllegalStateException e) {\n    // heap empty; nothing to remove\n}","preventionTips":["Bound drain loops by a size captured before the loop, not by a while(true).","Wrap remove() in a helper returning Optional<T>.","Treat emptiness as an expected state, not an error."],"tags":["heap","empty-state","illegal-state","generic-heap"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}