apache/beam · error · UnsupportedOperationException

Iterator does not support remove

Error message

Iterator does not support remove

What it means

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.

Solutions

  1. Do not call remove(); collect into a new list and remove from that copy instead.
  2. Copy the iterator's contents into an ArrayList first and iterate the copy with removal support.
  3. Filter upstream before sorting (e.g. with a Filter transform) rather than removing during iteration.

Example fix

// before
Iterator<KV<String, Integer>> it = iterable.iterator();
it.next();
it.remove(); // throws

// after
List<KV<String, Integer>> copy = Lists.newArrayList(iterable);
Iterator<KV<String, Integer>> it = copy.iterator();
it.next();
it.remove(); // safe, mutates the copy
Defensive patterns

Strategy: try-catch

Try / catch

try {
  iterator.remove();
} catch (UnsupportedOperationException e) {
  // fall back to building a filtered copy
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/7fe37ca9dd356982. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/extensions/sorter/src/main/java/org/apache/beam/sdk/extensions/sorter/SortValues.java:224

      public boolean hasNext() {
        return iterator.hasNext();
      }

      @Override
      public KV<SecondaryKeyT, ValueT> next() {
        KV<byte[], byte[]> next = iterator.next();
        try {
          SecondaryKeyT secondaryKey = elementOf(keyCoder, next.getKey());
          ValueT value = elementOf(valueCoder, next.getValue());
          return KV.of(secondaryKey, value);
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException("Iterator does not support remove");
      }
    }
  }
}

View on GitHub (pinned to 12126d8942)