{"record":{"id":"d2c7ed504425c9d2","repo":"apache/beam","slug":"duplicate-values-for-key","errorCode":null,"errorMessage":"Duplicate values for <key>","messagePattern":"Duplicate values for <key>","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java","lineNumber":1627,"sourceCode":"        TypeDescriptorSupplier<V> valueTypeDescriptorSupplier) {\n      this.keyTypeDescriptorSupplier = keyTypeDescriptorSupplier;\n      this.valueTypeDescriptorSupplier = valueTypeDescriptorSupplier;\n    }\n\n    @Override\n    public Materialization<MultimapView<Void, KV<K, V>>> getMaterialization() {\n      return Materializations.multimap();\n    }\n\n    @Override\n    public Map<K, V> apply(MultimapView<Void, KV<K, V>> primitiveViewT) {\n      // TODO: https://github.com/apache/beam/issues/18569 - fix this so that we aren't relying on\n      // Java equality and are\n      // using structural value equality.\n      Map<K, V> map = new HashMap<>();\n      for (KV<K, V> elem : primitiveViewT.get(null)) {\n        if (map.containsKey(elem.getKey())) {\n          throw new IllegalArgumentException(\"Duplicate values for \" + elem.getKey());\n        }\n        map.put(elem.getKey(), elem.getValue());\n      }\n      return Collections.unmodifiableMap(map);\n    }\n\n    @Override\n    public TypeDescriptor<Map<K, V>> getTypeDescriptor() {\n      return TypeDescriptors.maps(\n          keyTypeDescriptorSupplier.get(), valueTypeDescriptorSupplier.get());\n    }\n  }\n\n  /**\n   * Implementation which is able to adapt an iterable materialization to an in-memory {@code Map<K,\n   * V>}.\n   *\n   * <p>For internal use only.","sourceCodeStart":1609,"sourceCodeEnd":1645,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java#L1609-L1645","documentation":"While flattening the multimap materialization into a Map<K,V>, the view apply found the same key more than once among the KV entries. Map views require unique keys; the duplicate <key> entries in the input multimap are at fault (this remains a known issue around Java vs structural equality, per the adjacent TODO).","triggerScenarios":"Calling list.remove(i) on the List obtained from a Beam side input view.","commonSituations":"Deleting an entry from a side input list during element processing; refactoring old ArrayList-based code to use Beam views without adjusting mutability assumptions.","solutions":["Copy into a new ArrayList and call remove on the copy.","Use stream filter to build a list without the unwanted element (or by index: IntStream.range filtering).","Remove the elements upstream with a Beam Filter transform before creating the view."],"exampleFix":"// before\nsideInputList.remove(i);\n// after\nList<T> mutable = new ArrayList<>(sideInputList);\nmutable.remove(i);","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  viewedList.remove(i);\n} catch (UnsupportedOperationException e) {\n  List<T> copy = new ArrayList<>(viewedList);\n  copy.remove(i);\n  viewedList = copy;\n}","preventionTips":["Filter out unwanted elements upstream with a Beam Filter transform.","Copy before remove when operating at consumption time."],"tags":["java","apache-beam","immutable-collection","side-input","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}