stanfordnlp/CoreNLP · error · UnsupportedOperationException

Cannot remove from values collection

Error message

Cannot remove from values collection

What it means

The counter's values collection view exposes an Iterator whose remove() is intentionally unsupported. Removing through this iterator would desynchronize the wrapper's cached total from the underlying map, so the library throws UnsupportedOperationException instead.

Solutions

  1. Iterate the key set, collect the keys whose values qualify, then remove via counter.remove(key).
  2. Use Counters.retainKeys / threshold filtering utilities.
  3. Construct a filtered copy of the Counter instead of mutating the values view.

Example fix

// before
Iterator<Double> vit = counter.values().iterator();
while (vit.hasNext()) { if (vit.next() < 0.5) vit.remove(); } // throws
// after
counter.keySet().removeIf(k -> counter.getCount(k) < 0.5); // or collect + counter.remove
Defensive patterns

Strategy: validation

Validate before calling

// treat counter.values() as read-only; remove via keys:
List<E> victims = counter.keySet().stream()
    .filter(k -> counter.getCount(k) < threshold)
    .collect(Collectors.toList());

Try / catch

try {
  valuesIterator.remove();
} catch (UnsupportedOperationException e) {
  // fall back: remove owning key
  counter.remove(associatedKey);
}

Prevention

When it happens

Trigger: Calling remove() on the Iterator obtained from iterating the counter's values collection (e.g. `counter.values().iterator().remove()` or removing inside a values() loop).

Common situations: Trying to drop low-value entries by removing values directly while iterating; works on a HashMap values() view but not this Counter 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


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/d986aa893d157c5b. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Counters.java:2845

      }

      public Collection<Double> values() {
        return new AbstractCollection<Double>() {
          @Override
          public Iterator<Double> iterator() {
            return new Iterator<Double>() {
              final Iterator<N> it = map.values().iterator();

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

              public Double next() {
                return it.next().doubleValue();
              }

              public void remove() {
                throw new UnsupportedOperationException("Cannot remove from values collection");
              }
            };
          }

          @Override
          public int size() {
            return map.size();
          }
        };
      }

      /**
       * {@inheritDoc}
       */
      public void prettyLog(RedwoodChannels channels, String description) {
        PrettyLogger.log(channels, description, map);
      }
    };

View on GitHub (pinned to 1b7edd19c4)