{"record":{"id":"7fe37ca9dd356982","repo":"apache/beam","slug":"iterator-does-not-support-remove","errorCode":null,"errorMessage":"Iterator does not support remove","messagePattern":"Iterator does not support remove","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sorter/src/main/java/org/apache/beam/sdk/extensions/sorter/SortValues.java","lineNumber":224,"sourceCode":"      public boolean hasNext() {\n        return iterator.hasNext();\n      }\n\n      @Override\n      public KV<SecondaryKeyT, ValueT> next() {\n        KV<byte[], byte[]> next = iterator.next();\n        try {\n          SecondaryKeyT secondaryKey = elementOf(keyCoder, next.getKey());\n          ValueT value = elementOf(valueCoder, next.getValue());\n          return KV.of(secondaryKey, value);\n        } catch (IOException e) {\n          throw new RuntimeException(e);\n        }\n      }\n\n      @Override\n      public void remove() {\n        throw new UnsupportedOperationException(\"Iterator does not support remove\");\n      }\n    }\n  }\n}\n","sourceCodeStart":206,"sourceCodeEnd":229,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sorter/src/main/java/org/apache/beam/sdk/extensions/sorter/SortValues.java#L206-L229","documentation":"The iterator returned over the sorted buffered elements deliberately does not implement removal; calling remove() throws UnsupportedOperationException. The sorted stream is produced once and consumed read-only, so mutation is not meaningful. This is an unconditional throw — the operation is simply not supported.","triggerScenarios":"Any call to Iterator.remove() on the iterator obtained from the sorted output of SortValues' buffered sorting (e.g. inside a DoFn iterating the sorted iterable, or via a library that mutates collections while iterating).","commonSituations":"Developers try to strip elements while iterating sorted results, or hand the iterator to utilities like Iterators.filter/consuming wrappers that call remove() as an optimization.","solutions":["Do not call remove(); collect into a new list and remove from that copy instead.","Copy the iterator's contents into an ArrayList first and iterate the copy with removal support.","Filter upstream before sorting (e.g. with a Filter transform) rather than removing during iteration."],"exampleFix":"// before\nIterator<KV<String, Integer>> it = iterable.iterator();\nit.next();\nit.remove(); // throws\n\n// after\nList<KV<String, Integer>> copy = Lists.newArrayList(iterable);\nIterator<KV<String, Integer>> it = copy.iterator();\nit.next();\nit.remove(); // safe, mutates the copy","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  iterator.remove();\n} catch (UnsupportedOperationException e) {\n  // fall back to building a filtered copy\n}","preventionTips":["Treat iterators over sorted Beam results as read-only","Copy into a mutable List before removing elements","Filter upstream with a Beam Filter transform instead of mutating during iteration"],"tags":["apache-beam","iterator","java","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"}