{"record":{"id":"60a99e5b80629ab2","repo":"stanfordnlp/CoreNLP","slug":"different-number-of-keys-and-values","errorCode":null,"errorMessage":"different number of keys and values.","messagePattern":"different number of keys and values\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/ArrayMap.java","lineNumber":96,"sourceCode":"\n  @SuppressWarnings(\"unchecked\")\n  public ArrayMap(int capacity) {\n    size = 0;\n    this.capacity = capacity;\n    entryArray = new Entry[capacity];\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  public ArrayMap(Map<? extends K, ? extends V> m) {\n\t  size = 0;\n\t  capacity = m.size();\n\t  entryArray = new Entry[m.size()];\n\t  this.putAll(m);\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  public ArrayMap(K[] keys, V[] values) {\n    if (keys.length!=values.length) throw new IllegalArgumentException(\"different number of keys and values.\");\n    size = keys.length;\n    capacity = size;\n    entryArray = new Entry[size];\n    for (int i=0; i<keys.length; i++) {\n      entryArray[i] = new Entry(keys[i], values[i]);\n    }\n  }\n\n  public static <K, V> ArrayMap<K, V> newArrayMap() {\n    return new ArrayMap<>();\n  }\n\n  public static <K, V> ArrayMap<K, V> newArrayMap(int capacity) {\n    return new ArrayMap<>(capacity);\n  }\n\n  @Override\n  public Set<Map.Entry<K,V>> entrySet() {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/ArrayMap.java#L78-L114","documentation":"ArrayMap's constructor ArrayMap(K[] keys, V[] values) builds a parallel-array-backed map from two arrays. Because entries are created pairwise, the two arrays must have identical lengths; the library throws IllegalArgumentException up front when keys.length != values.length to avoid silently dropping or mispairing data.","triggerScenarios":"Calling new ArrayMap<K,V>(keys, values) where the keys array and values array have different lengths (e.g. keys.length == 3 but values.length == 2).","commonSituations":"Hand-assembling key/value arrays where one list was edited without updating the other; filtering nulls or duplicates out of one array but not its partner; loading keys and values from separate config sources of different sizes.","solutions":["Check keys.length == values.length before constructing the ArrayMap and fix the data source that produced the shorter array.","Build the map via the no-arg constructor and putAll(m) from a real Map, which eliminates the parallel-array length coupling.","If arrays may legitimately differ, pad the shorter array or decide explicitly which trailing pairs to drop before construction."],"exampleFix":"// before\nArrayMap<String,Integer> m = new ArrayMap<>(new String[]{\"a\",\"b\",\"c\"}, new Integer[]{1,2});\n// after\nString[] keys = {\"a\",\"b\",\"c\"};\nInteger[] vals = {1,2,3};\nif (keys.length != vals.length) throw new IllegalArgumentException(\"key/value length mismatch\");\nArrayMap<String,Integer> m = new ArrayMap<>(keys, vals);","handlingStrategy":"validation","validationCode":"if (keys == null || values == null || keys.length != values.length) {\n  throw new IllegalArgumentException(\"keys and values must be non-null and same length\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  ArrayMap<K,V> m = new ArrayMap<>(keys, values);\n} catch (IllegalArgumentException e) {\n  // log pair-length mismatch and fall back to putAll from a Map\n}","preventionTips":["Construct keys and values arrays from a single source (e.g. iterate one Map) so lengths cannot diverge.","Assert keys.length == values.length in tests for any helper that builds parallel arrays.","Prefer putAll(Map) over the parallel-array constructor."],"tags":["java","collections","constructor-argument","length-mismatch"],"backgroundTag":"invalid-constructor-argument","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"}