{"record":{"id":"a7b09f20dd9f61ab","repo":"TheAlgorithms/Java","slug":"table-is-empty-cannot-delete","errorCode":null,"errorMessage":"Table is empty, cannot delete.","messagePattern":"Table is empty, cannot delete\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java","lineNumber":149,"sourceCode":"                newT.insertKey2HashTable(this.buckets[i]);\n            }\n        }\n        this.tableSize *= 2;\n        this.buckets = newT.buckets;\n        this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2;\n    }\n\n    /**\n     * Deletes a key from the hash table, marking its position as available.\n     *\n     * @param key the key to be deleted from the hash table\n     * @throws IllegalArgumentException if the table is empty or if the key is not found\n     */\n    public void deleteKeyFromHashTable(int key) {\n        Integer wrappedInt = key;\n        int hash = hashFunction1(key);\n        if (isEmpty()) {\n            throw new IllegalArgumentException(\"Table is empty, cannot delete.\");\n        }\n\n        if (Objects.equals(buckets[hash], wrappedInt)) {\n            buckets[hash] = emptySlot;\n            size--;\n            return;\n        }\n\n        hash = hashFunction2(key);\n        if (Objects.equals(buckets[hash], wrappedInt)) {\n            buckets[hash] = emptySlot;\n            size--;\n            return;\n        }\n        throw new IllegalArgumentException(\"Key \" + key + \" not found in the table.\");\n    }\n\n    /**","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java#L131-L167","documentation":"Thrown by `HashMapCuckooHashing.deleteKeyFromHashTable` when the table is empty (`isEmpty()` true). Deleting from an empty table is meaningless, so the method guards before looking up either bucket. This prevents confusing behavior where a stale `emptySlot` could be misinterpreted.","triggerScenarios":"Calling `deleteKeyFromHashTable(k)` when no keys have been inserted, or after all keys were already deleted.","commonSituations":"Calling delete before any insert; double-delete; a cleanup routine that runs even when the table is already empty.","solutions":["Guard with `!isEmpty()` before deleting","Track insert/delete counts at the caller to avoid redundant deletes","Short-circuit cleanup loops when the table is empty"],"exampleFix":"// before\nmap.deleteKeyFromHashTable(k);\n// after\nif (!map.isEmpty()) {\n    map.deleteKeyFromHashTable(k);\n}","handlingStrategy":"validation","validationCode":"if (!map.isEmpty()) {\n    map.deleteKeyFromHashTable(key);\n}","typeGuard":null,"tryCatchPattern":"try {\n    map.deleteKeyFromHashTable(key);\n} catch (IllegalArgumentException e) {\n    // table is empty\n}","preventionTips":["Check isEmpty() before delete in cleanup code","Avoid blind deletes from possibly-empty tables"],"tags":["hashmap","cuckoo-hashing","state","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}