{"record":{"id":"cbc189c413b460fe","repo":"TheAlgorithms/Java","slug":"minpriorityqueue-is-empty-cannot-peek","errorCode":null,"errorMessage":"MinPriorityQueue is empty. Cannot peek.","messagePattern":"MinPriorityQueue is empty\\. Cannot peek\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java","lineNumber":69,"sourceCode":"            if (this.heap[k] < this.heap[k / 2]) {\n                int temp = this.heap[k];\n                this.heap[k] = this.heap[k / 2];\n                this.heap[k / 2] = temp;\n            }\n            k = k / 2;\n        }\n        this.size++;\n    }\n\n    /**\n     * Retrieves the highest priority value (the minimum) without removing it.\n     *\n     * @return the minimum value in the queue\n     * @throws IllegalStateException if the queue is empty\n     */\n    public int peek() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"MinPriorityQueue is empty. Cannot peek.\");\n        }\n        return this.heap[1];\n    }\n\n    /**\n     * Checks whether the queue is empty.\n     *\n     * @return true if the queue is empty, false otherwise\n     */\n    public boolean isEmpty() {\n        return size == 0;\n    }\n\n    /**\n     * Checks whether the queue is full.\n     *\n     * @return true if the queue is full, false otherwise\n     */","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java#L51-L87","documentation":"Thrown by MinPriorityQueue.peek() when isEmpty() returns true. The queue stores the minimum at heap[1] using 1-based array indexing, so peeking an empty queue would read an uninitialized slot. The guard enforces the non-empty precondition before reading the root.","triggerScenarios":"Calling peek on a freshly constructed queue with no inserts. Calling peek after draining all elements via delete.","commonSituations":"Polling a queue that may not yet be populated by a producer. Top-of-loop read before the first insert. Empty-input edge case in a merge or scheduling algorithm.","solutions":["Guard with isEmpty(): `return pq.isEmpty() ? OptionalInt.empty() : OptionalInt.of(pq.peek());`.","Ensure at least one insert precedes any peek.","Catch IllegalStateException and return a sentinel/empty result.","Structure the consumer to only run after the producer signals population."],"exampleFix":"// before\nint min = pq.peek();\n\n// after\nif (pq.isEmpty()) {\n    return OptionalInt.empty();\n}\nreturn OptionalInt.of(pq.peek());","handlingStrategy":"validation","validationCode":"if (!pq.isEmpty()) {\n    return pq.peek();\n}\nreturn OptionalInt.empty();","typeGuard":null,"tryCatchPattern":"try {\n    return OptionalInt.of(pq.peek());\n} catch (IllegalStateException e) {\n    return OptionalInt.empty();\n}","preventionTips":["Guard peek with isEmpty().","Return an Optional-like result to callers.","Ensure a producer has populated the queue before the consumer peeks."],"tags":["priority-queue","empty-state","data-structure","precondition","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}