{"record":{"id":"b3fa1e3f17902871","repo":"stanfordnlp/CoreNLP","slug":"error-cannot-insert-multiple-keys-with-the-same-v","errorCode":null,"errorMessage":"Error: cannot insert multiple keys with the same value","messagePattern":"Error: cannot insert multiple keys with the same value","errorType":"exception","errorClass":"OneToOneMapException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/OneToOneMap.java","lineNumber":60,"sourceCode":"  {\n    return m_leftAsKey.isEmpty();\n  }\n\n\n\n  public int size()\n  {\n    return m_leftAsKey.size();\n  }\n\n  public void put(L l,R r) throws OneToOneMapException\n  {\n    boolean hasLeft = m_leftAsKey.containsKey(l);\n    boolean hasRight = m_rightAsKey.containsKey(r);\n\n\n    if(hasLeft != hasRight)\n      throw new OneToOneMapException(\"Error: cannot insert multiple keys with the same value\");\n\n    m_leftAsKey.put(l,r);\n    m_rightAsKey.put(r, l);\n  }\n\n\n\n  public R getLeftAsKey(L l)\n  {\n    return m_leftAsKey.get(l);\n  }\n\n\n\n  public L getRightAsKey(R r)\n  {\n    return m_rightAsKey.get(r);\n  }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/OneToOneMap.java#L42-L78","documentation":"OneToOneMap maintains a strict bijection: each left value maps to exactly one right value and vice versa. put(l, r) throws OneToOneMapException when adding an entry would reuse either side in a way that breaks the bijection (hasLeft != hasRight indicates one of l or r is already bound to a different partner). It refuses partial overwrites so the map never silently loses a mapping.","triggerScenarios":"Calling put(l, r) where l is already mapped to a different r, or r is already mapped to a different l — i.e. any duplicate left or duplicate right key.","commonSituations":"Loading a mapping table (e.g. label-to-id) with duplicate values on either side; merging two datasets whose mappings collide; assuming put behaves like HashMap.put and overwrites.","solutions":["Remove the existing conflicting entry first before inserting the new pair","Deduplicate your input data so each left and right value appears at most once","Call containsLeft/containsRight (or check the backing maps) before put to detect collisions","If you need many-to-one semantics, use a regular HashMap instead of OneToOneMap"],"exampleFix":"// before\nmap.put(\"a\", 1); map.put(\"b\", 1); // duplicate right value\n// after\nif (map.containsRight(1)) { map.removeRight(1); }\nmap.put(\"b\", 1); // or use HashMap for non-bijective mappings","handlingStrategy":"validation","validationCode":"// before put\nif (map.containsKey(l) || map.containsValue(r)) {\n  throw new IllegalArgumentException(\"Collision: \" + l + \" or \" + r + \" already bound\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  map.put(l, r);\n} catch (OneToOneMapException e) {\n  logger.warning(\"Bijective conflict for (\" + l + \", \" + r + \"); removing old bindings\");\n  // remove conflicting entries then retry\n}","preventionTips":["Deduplicate input data on both left and right values before building the map","Do not assume HashMap overwrite semantics — OneToOneMap refuses collisions","Check containsLeft/containsRight before inserting","Use a plain HashMap if you do not need invertibility"],"tags":["map","bidirectional","duplicate-key","data-structure"],"backgroundTag":"conflicting-config-options","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}