stanfordnlp/CoreNLP · error · UnsupportedOperationException
Cannot remove from key set
Error message
Cannot remove from key set
What it means
Counters.java wraps a counter's key set in an Iterator whose remove() is deliberately unsupported. The library exposes the key set for iteration only; mutating the backing counter through the iterator would corrupt the internal total maintained by the wrapper. Call UnsupportedOperationException to signal that structural removal must go through the Counter API instead.
Solutions
- Collect keys to remove into a separate list, then call counter.remove(key) after iteration.
- Use Counters.retainKeys(counter, predicate) or similar filtering helper instead of iterator.remove().
- Build a new Counter containing only the keys you want to keep.
Example fix
// before
for (Iterator<String> it = counter.keySet().iterator(); it.hasNext();) {
String k = it.next();
if (counter.getCount(k) < 0.5) it.remove(); // throws
}
// after
List<String> toRemove = new ArrayList<>();
for (String k : counter.keySet()) {
if (counter.getCount(k) < 0.5) toRemove.add(k);
}
toRemove.forEach(counter::remove); Defensive patterns
Strategy: validation
Validate before calling
if (counter == null || counter.keySet() == null) throw new IllegalArgumentException("counter must not be null");
// plan removals via keys, never via iterator.remove() Try / catch
// should never rely on catching; structure code to avoid iterator.remove()
try {
keySetIterator.remove();
} catch (UnsupportedOperationException e) {
pendingRemovals.add(currentKey); // defer removal
} Prevention
- Never call remove() on iterators of Counter collection views; treat them as read-only.
- Collect keys during iteration and remove after the loop with counter.remove(key).
- Prefer Counters.retainKeys/filtering helpers over manual iteration.
When it happens
Trigger: Calling java.util.Iterator.remove() while iterating counter.keySet() (or any view backed by this iterator), e.g. `for (E k : counter.keySet()) { it.remove(); }` or removing via the for-each collection's iterator.
Common situations: Developers pruning keys below a threshold or deleting zero-count entries while iterating a Counter, a pattern that works on plain HashMap keySets but not on this view.
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
- Cannot remove from values collection
- iterator is not supported for…
- toArray is not supported for…
- Remove is not supported for…
- retainAll is not supported for…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/9e922cd8367cd7e3.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/Counters.java:2767
}
public Set<E> keySet() {
return new AbstractSet<E>() {
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Iterator<E> it = map.keySet().iterator();
public boolean hasNext() {
return it.hasNext();
}
public E next() {
return it.next();
}
public void remove() {
throw new UnsupportedOperationException("Cannot remove from key set");
}
};
}
@Override
public int size() {
return map.size();
}
};
}
public double remove(E key) {
final Number removed = map.remove(key);
if (removed != null) {
final double rv = removed.doubleValue();
total -= rv;
return rv;
}View on GitHub (pinned to 1b7edd19c4)