{"record":{"id":"39ec92780511d055","repo":"TheAlgorithms/Java","slug":"key-already-exists-duplicates-are-not-allowed","errorCode":null,"errorMessage":"Key already exists; duplicates are not allowed.","messagePattern":"Key already exists; duplicates are not allowed\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java","lineNumber":85,"sourceCode":"     * it into its alternate location. If the insertion process exceeds the threshold,\n     * the table is resized.\n     *\n     * @param key the key to be inserted into the hash table\n     * @throws IllegalArgumentException if the key already exists in the table\n     */\n    public void insertKey2HashTable(int key) {\n        Integer wrappedInt = key;\n        Integer temp;\n        int hash;\n        int loopCounter = 0;\n\n        if (isFull()) {\n            System.out.println(\"Hash table is full, lengthening & rehashing table\");\n            reHashTableIncreasesTableSize();\n        }\n\n        if (checkTableContainsKey(key)) {\n            throw new IllegalArgumentException(\"Key already exists; duplicates are not allowed.\");\n        }\n\n        while (loopCounter <= thresh) {\n            loopCounter++;\n            hash = hashFunction1(key);\n\n            if ((buckets[hash] == null) || Objects.equals(buckets[hash], emptySlot)) {\n                buckets[hash] = wrappedInt;\n                size++;\n                checkLoadFactor();\n                return;\n            }\n\n            temp = buckets[hash];\n            buckets[hash] = wrappedInt;\n            wrappedInt = temp;\n            hash = hashFunction2(temp);\n            if (Objects.equals(buckets[hash], emptySlot)) {","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java#L67-L103","documentation":"Thrown by `HashMapCuckooHashing.insertKey2HashTable` when the key is already present. Cuckoo hashing stores each key in exactly one of two possible buckets, so a duplicate would violate that invariant; the insert method checks `checkTableContainsKey(key)` before placing the key and refuses duplicates.","triggerScenarios":"Calling `insertKey2HashTable(k)` twice with the same `k`, or inserting a key that an earlier insert/rehash already stored.","commonSituations":"Re-ingesting the same records in a pipeline; idempotency not handled at the caller; re-insertion after a failed delete.","solutions":["Check `checkTableContainsKey(key)` before inserting","De-duplicate input data upstream","Treat re-insertion as a no-op by guarding with a contains check"],"exampleFix":"// before\nmap.insertKey2HashTable(k);\n// after\nif (!map.checkTableContainsKey(k)) {\n    map.insertKey2HashTable(k);\n}","handlingStrategy":"validation","validationCode":"if (!map.checkTableContainsKey(key)) {\n    map.insertKey2HashTable(key);\n}","typeGuard":null,"tryCatchPattern":"try {\n    map.insertKey2HashTable(key);\n} catch (IllegalArgumentException e) {\n    // key already present\n}","preventionTips":["Guard inserts with a contains check for idempotent pipelines","De-duplicate source data before bulk insertion"],"tags":["hashmap","cuckoo-hashing","duplicate","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}