TheAlgorithms/Java · error · IllegalArgumentException

Key {key} not found in the table.

Error message

Key {key} not found in the table.

What it means

Thrown by `HashMapCuckooHashing.deleteKeyFromHashTable` when the key is not found in either of its two cuckoo buckets (`hashFunction1` or `hashFunction2`). Cuckoo hashing guarantees a present key lives in one of exactly two slots, so absence in both means the key was never inserted (or was already deleted, its slot now `emptySlot`).

Source

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

        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.");
    }

    /**
     * Displays the hash table contents, bucket by bucket.
     */
    public void displayHashtable() {
        for (int i = 0; i < tableSize; i++) {
            if ((buckets[i] == null) || Objects.equals(buckets[i], emptySlot)) {
                System.out.println("Bucket " + i + ": Empty");
            } else {
                System.out.println("Bucket " + i + ": " + buckets[i].toString());
            }
        }
        System.out.println();
    }

    /**
     * Finds the index of a given key in the hash table.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Verify the key exists with `checkTableContainsKey(k)` before deleting
  2. Track which keys you inserted at the caller
  3. Treat 'not found' as a no-op rather than an error at the call site

Example fix

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

Strategy: validation

Validate before calling

if (map.checkTableContainsKey(key)) {
    map.deleteKeyFromHashTable(key);
}

Try / catch

try {
    map.deleteKeyFromHashTable(key);
} catch (IllegalArgumentException e) {
    // key not present
}

Prevention

When it happens

Trigger: Calling `deleteKeyFromHashTable(k)` for a `k` that was never inserted, or that was already deleted.

Common situations: Deleting a key from stale data; double-delete; deleting a key that was never there; retrying a delete that already succeeded.

Related errors


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