{"record":{"id":"fad81098cdded73a","repo":"TheAlgorithms/Java","slug":"queue-is-empty-fad810","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java","lineNumber":56,"sourceCode":"    }\n\n    /**\n     * Removes and returns the element at the front of the queue.\n     * If `dequeueStk` is empty, it transfers all elements from\n     * `enqueueStk` to `dequeueStk` to maintain the correct FIFO\n     * (First-In-First-Out) order before popping.\n     *\n     * @return The element at the front of the queue.\n     * @throws NoSuchElementException If the queue is empty.\n     */\n    public T get() {\n        if (dequeueStk.isEmpty()) {\n            while (!enqueueStk.isEmpty()) {\n                dequeueStk.push(enqueueStk.pop());\n            }\n        }\n        if (dequeueStk.isEmpty()) {\n            throw new NoSuchElementException(\"Queue is empty\");\n        }\n        return dequeueStk.pop();\n    }\n\n    /**\n     * Returns the total number of elements currently in the queue.\n     * This is the sum of the sizes of both stacks.\n     *\n     * @return The number of elements in the queue.\n     */\n    public int size() {\n        return enqueueStk.size() + dequeueStk.size();\n    }\n\n    /**\n     * Returns a string representation of the queue, showing the elements\n     * in the correct order (from front to back).\n     * The `dequeueStk` is first cloned, and then all elements from the","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java#L38-L74","documentation":"Thrown by QueueByTwoStacks.get() as a NoSuchElementException when both internal stacks are empty. get() first transfers any pending elements from enqueueStk to dequeueStk; only if dequeueStk is still empty afterward does it throw. This means the queue is genuinely empty (no put() since the last full drain).","triggerScenarios":"Calling get() on a freshly constructed QueueByTwoStacks, or calling get() more times than put(). The transfer step means a get() right after put() calls succeeds; the throw only happens when there is nothing on either stack.","commonSituations":"Consumer thread calling get() before the producer has put() anything; drain loop that calls get() one time too many; misuse where get() is the only emptiness signal; assuming get() blocks (it does not — this is non-blocking).","solutions":["Track emptiness via size() (enqueueStk + dequeueStk sizes) and guard get() with size() > 0.","Bound get() loops by the number of put() calls actually made.","Catch NoSuchElementException when an empty queue is an expected outcome."],"exampleFix":"// before\nT v = queue.get(); // throws when empty\n\n// after\nT v = null;\nif (queue.size() > 0) {\n    v = queue.get();\n}","handlingStrategy":"validation","validationCode":"T v = null;\nif (queue.size() > 0) {\n    v = queue.get();\n}","typeGuard":"boolean nonEmpty = queue.size() > 0;","tryCatchPattern":"null","preventionTips":["Guard get() with size() > 0 — get() is non-blocking and throws rather than waiting.","Bound get() loops by the number of put() calls actually made.","Do not use get() as the sole emptiness signal; track size explicitly."],"tags":["queue","data-structure","underflow","empty-state","two-stacks","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}