{"record":{"id":"4e4e2e188001933c","repo":"TheAlgorithms/Java","slug":"duplicate-key-key","errorCode":null,"errorMessage":"Duplicate key: {key}","messagePattern":"Duplicate key: (.+?)","errorType":"exception","errorClass":"DuplicateKeyException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java","lineNumber":242,"sourceCode":"            } else if (root.right.key < key) {\n                root.right.right = splay(root.right.right, key);\n                root = rotateLeft(root);\n            }\n            return (root.right == null) ? root : rotateLeft(root);\n        }\n    }\n\n    private Node insertRec(Node root, final int key) {\n        if (root == null) {\n            return new Node(key);\n        }\n\n        if (key < root.key) {\n            root.left = insertRec(root.left, key);\n        } else if (key > root.key) {\n            root.right = insertRec(root.right, key);\n        } else {\n            throw new DuplicateKeyException(\"Duplicate key: \" + key);\n        }\n\n        return root;\n    }\n\n    public static class EmptyTreeException extends RuntimeException {\n        private static final long serialVersionUID = 1L;\n\n        public EmptyTreeException(String message) {\n            super(message);\n        }\n    }\n\n    public static class DuplicateKeyException extends RuntimeException {\n        private static final long serialVersionUID = 1L;\n\n        public DuplicateKeyException(String message) {\n            super(message);","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/SplayTree.java#L224-L260","documentation":"SplayTree does not allow duplicate keys. insertRec(Node, int) traverses the BST and, when it encounters a node whose key equals the key being inserted, throws DuplicateKeyException (a RuntimeException subclass) with the offending key in the message. This enforces set semantics for the tree.","triggerScenarios":"Calling tree.insert(key) when a node with that exact key already exists in the tree — including re-inserting after a prior successful insert of the same value.","commonSituations":"Ingesting data from a source with non-unique identifiers; retry/replay logic that re-inserts keys; concurrent-feeling code paths where two callers insert the same key.","solutions":["Call tree.search(key) first and skip insertion if it returns true.","Catch DuplicateKeyException at the call site if duplicates are acceptable and should be silently ignored.","Deduplicate your input collection before inserting into the tree."],"exampleFix":"// before\ntree.insert(10);\ntree.insert(10); // throws DuplicateKeyException\n\n// after\ntree.insert(10);\nif (!tree.search(10)) {\n    tree.insert(10);\n}","handlingStrategy":"validation","validationCode":"if (!tree.search(key)) {\n    tree.insert(key);\n}","typeGuard":null,"tryCatchPattern":"try {\n    tree.insert(key);\n} catch (SplayTree.DuplicateKeyException e) {\n    // key already present — treat as no-op or update\n}","preventionTips":["Deduplicate input data before inserting into the tree.","Always check search(key) before insert if duplicates are possible."],"tags":["splay-tree","duplicate-key","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}