TheAlgorithms/Java · error · IllegalArgumentException

Table is empty; cannot find keys.

Error message

Table is empty; cannot find keys.

What it means

Thrown by HashMapCuckooHashing.findKeyInTable(int) when the table holds no keys. Cuckoo hashing places each key in one of exactly two bucket positions computed by hashFunction1/hashFunction2, so a lookup is only meaningful when at least one bucket is occupied. isEmpty() scans every bucket and returns true only when all are null, so the guard fires on a never-seeded table or one fully drained by deletions.

Source

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

                System.out.println("Bucket " + i + ": " + buckets[i].toString());
            }
        }
        System.out.println();
    }

    /**
     * Finds the index of a given key in the hash table.
     *
     * @param key the key to be found
     * @return the index where the key is located
     * @throws IllegalArgumentException if the table is empty or the key is not found
     */
    public int findKeyInTable(int key) {
        Integer wrappedInt = key;
        int hash = hashFunction1(key);

        if (isEmpty()) {
            throw new IllegalArgumentException("Table is empty; cannot find keys.");
        }

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

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

    /**
     * Checks if the given key is present in the hash table.
     *
     * @param key the key to be checked

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard the call with `if (!h.isEmpty()) h.findKeyInTable(key);` so an empty table skips the lookup.
  2. Ensure at least one key is inserted via insertKey2HashTable before any findKeyInTable call in the same flow.
  3. Wrap the call in try/catch(IllegalArgumentException) and treat the exception as 'key absent'.

Example fix

// before
int idx = h.findKeyInTable(key);

// after
int idx = h.isEmpty() ? -1 : (h.checkTableContainsKey(key) ? h.findKeyInTable(key) : -1);
Defensive patterns

Strategy: validation

Validate before calling

// Run before findKeyInTable
if (h.isEmpty()) {
    // no keys present; do not call findKeyInTable
    return -1; // or handle 'empty' case
}
int idx = h.findKeyInTable(key);

Try / catch

try {
    int idx = h.findKeyInTable(key);
} catch (IllegalArgumentException e) {
    // table empty OR key absent; treat as not-found
}

Prevention

When it happens

Trigger: Calling findKeyInTable(key) immediately after `new HashMapCuckooHashing(7)` with no prior insertKey2HashTable calls; calling it after a loop of deleteKeyFromHashTable has removed every key so size reaches 0 and all buckets are null again.

Common situations: Application startup paths that query before seeding data; test scaffolding that asserts on an unpopulated map; bulk-delete routines that then attempt a lookup without re-checking occupancy.

Related errors


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