{"record":{"id":"1634f97a6bd23ce5","repo":"TheAlgorithms/Java","slug":"item-not-found-in-the-heap","errorCode":null,"errorMessage":"Item not found in the heap","messagePattern":"Item not found in the heap","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java","lineNumber":144,"sourceCode":"     * @param j index of the second item\n     */\n    private void swap(int i, int j) {\n        T ith = this.data.get(i);\n        T jth = this.data.get(j);\n        this.data.set(i, jth);\n        this.data.set(j, ith);\n        map.put(ith, j);\n        map.put(jth, i);\n    }\n\n    /**\n     * Updates the priority of the specified item by restoring the heap property.\n     *\n     * @param item the item whose priority is to be updated\n     */\n    public void updatePriority(T item) {\n        if (!map.containsKey(item)) {\n            throw new IllegalArgumentException(\"Item not found in the heap\");\n        }\n        int index = map.get(item);\n        upHeapify(index);\n    }\n}\n","sourceCodeStart":126,"sourceCodeEnd":150,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java#L126-L150","documentation":"Thrown by GenericHeap.updatePriority(T) when the item is not a key in the internal HashMap<T,Integer> index. The heap tracks element positions by equality via the map, so an item that was never added, was already removed, or is a different instance whose equals() does not match a stored key fails the containsKey check. Note GenericHeap does not expose a public contains() method, so callers must track membership themselves.","triggerScenarios":"Calling updatePriority on an item never inserted via add(); on an item already returned by remove() (it is dropped from the map); on a newly constructed object that is equal-but-distinct when equals/hashCode are inconsistent with storage.","commonSituations":"Dijkstra/A*-style relaxation where a node was already extracted; stale references kept after removal; mutable objects whose hashCode changed after insertion (map lookup then misses).","solutions":["Only call updatePriority on the exact object reference previously passed to add() and not yet removed.","Maintain your own Set<T> of live items and check contains() before calling updatePriority.","Ensure inserted objects have stable equals/hashCode (do not mutate fields used by hashCode after insertion)."],"exampleFix":"// before\nheap.updatePriority(item);\n\n// after\nif (liveItems.contains(item)) {\n    heap.updatePriority(item);\n}","handlingStrategy":"validation","validationCode":"// GenericHeap has no public contains(); track membership yourself\nSet<T> live = new HashSet<>();\n// on add: live.add(item);  on remove: live.remove(returned);\nif (live.contains(item)) {\n    heap.updatePriority(item);\n}","typeGuard":null,"tryCatchPattern":"try {\n    heap.updatePriority(item);\n} catch (IllegalArgumentException e) {\n    // item not in heap; re-insert or skip\n}","preventionTips":["Only call updatePriority on the exact reference previously added and not yet removed.","Keep inserted objects' equals/hashCode stable (do not mutate fields used by hashCode).","Maintain a parallel live-set to mirror the heap's membership."],"tags":["heap","membership","priority-update","generic-heap"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}