TheAlgorithms/Java · warning · IllegalArgumentException

Table is empty, cannot delete.

Error message

Table is empty, cannot delete.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java:149

                newT.insertKey2HashTable(this.buckets[i]);
            }
        }
        this.tableSize *= 2;
        this.buckets = newT.buckets;
        this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2;
    }

    /**
     * Deletes a key from the hash table, marking its position as available.
     *
     * @param key the key to be deleted from the hash table
     * @throws IllegalArgumentException if the table is empty or if the key is not found
     */
    public void deleteKeyFromHashTable(int key) {
        Integer wrappedInt = key;
        int hash = hashFunction1(key);
        if (isEmpty()) {
            throw new IllegalArgumentException("Table is empty, cannot delete.");
        }

        if (Objects.equals(buckets[hash], wrappedInt)) {
            buckets[hash] = emptySlot;
            size--;
            return;
        }

        hash = hashFunction2(key);
        if (Objects.equals(buckets[hash], wrappedInt)) {
            buckets[hash] = emptySlot;
            size--;
            return;
        }
        throw new IllegalArgumentException("Key " + key + " not found in the table.");
    }

    /**

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard with `!isEmpty()` before deleting
  2. Track insert/delete counts at the caller to avoid redundant deletes
  3. Short-circuit cleanup loops when the table is empty

Example fix

// before
map.deleteKeyFromHashTable(k);
// after
if (!map.isEmpty()) {
    map.deleteKeyFromHashTable(k);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!map.isEmpty()) {
    map.deleteKeyFromHashTable(key);
}

Try / catch

try {
    map.deleteKeyFromHashTable(key);
} catch (IllegalArgumentException e) {
    // table is empty
}

Prevention

When it happens

Trigger: Calling `deleteKeyFromHashTable(k)` when no keys have been inserted, or after all keys were already deleted.

Common situations: Calling delete before any insert; double-delete; a cleanup routine that runs even when the table is already empty.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/a7b09f20dd9f61ab. Report an issue: GitHub.