{"record":{"id":"7ddbf24fb630f60b","repo":"TheAlgorithms/Java","slug":"element-not-in-queue","errorCode":null,"errorMessage":"Element not in queue","messagePattern":"Element not in queue","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java","lineNumber":174,"sourceCode":"    }\n\n    // ------------------------------------------------------------------------------------\n    // Key update API\n    // ------------------------------------------------------------------------------------\n\n    /**\n     * Changes comparator-relevant fields of {@code e} via the provided {@code mutator},\n     * then restores the heap in O(log n) by bubbling in the correct direction.\n     *\n     * <p><b>IMPORTANT:</b> The mutator must not change {@code equals/hashCode} of {@code e}\n     * if you migrate this implementation to value-based indexing (HashMap).\n     *\n     * @throws IllegalArgumentException if {@code e} is not in the queue\n     */\n    public void changeKey(E e, Consumer<E> mutator) {\n        Integer i = index.get(e);\n        if (i == null) {\n            throw new IllegalArgumentException(\"Element not in queue\");\n        }\n        // Mutate fields used by comparator (do NOT mutate equality/hash if using value-based map)\n        mutator.accept(e);\n        // Try bubbling up; if no movement occurred, bubble down.\n        if (!siftUp(i)) {\n            siftDown(i);\n        }\n    }\n\n    /**\n     * Faster variant if the new key is strictly smaller (higher priority).\n     * Performs a single sift-up (O(log n)).\n     */\n    public void decreaseKey(E e, Consumer<E> mutator) {\n        Integer i = index.get(e);\n        if (i == null) {\n            throw new IllegalArgumentException(\"Element not in queue\");\n        }","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java#L156-L192","documentation":"Thrown by IndexedPriorityQueue.changeKey(E, Consumer) when index.get(e) returns null. The index is an IdentityHashMap, so membership is tested by reference identity, not equals. A null index means the element was never inserted, was already removed/polled, or a different instance (even an equal one) was passed.","triggerScenarios":"Calling changeKey on an element never inserted; on an element already returned by a poll/remove; passing a freshly reconstructed object that equals a stored one but is not the same instance.","commonSituations":"Dijkstra relaxation after a node was settled and extracted; reusing element references after they were polled; value-based lookups that assume equals semantics.","solutions":["Keep and reuse the exact object references returned/stored by the queue and only call changeKey on those.","Track live membership in your own IdentityHashMap<element,Boolean> mirroring insertion/removal.","Do not reconstruct equal-but-different element instances and pass them to changeKey; identity will not match."],"exampleFix":"// before\nipq.changeKey(node, n -> n.dist = newDist);\n\n// after\nif (ipq.contains(node)) {  // or your own live-set check\n    ipq.changeKey(node, n -> n.dist = newDist);\n}","handlingStrategy":"validation","validationCode":"// IdentityHashMap index: use reference-equality live set\nIdentityHashMap<E, Boolean> live = new IdentityHashMap<>();\n// on insert: live.put(e, true);  on remove/poll: live.remove(e);\nif (live.containsKey(e)) {\n    ipq.changeKey(e, mutator);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ipq.changeKey(e, mutator);\n} catch (IllegalArgumentException ex) {\n    // element not in queue; re-insert or ignore\n}","preventionTips":["Reuse stored element references; never reconstruct equal copies for key changes.","Track live membership in an IdentityHashMap mirroring insertion/removal.","In graph algorithms, mark nodes as settled and skip changeKey after extraction."],"tags":["heap","priority-queue","identity","change-key"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}