stanfordnlp/CoreNLP · error · UnsupportedOperationException

Remove is not supported for…

Error message

Remove is not supported for PhraseTable.PhraseStringCollection

What it means

PhraseTable.PhraseStringCollection is a read/write Collection view over a PhraseTable that supports add and clear but not element removal. The AbstractCollection-inherited remove(Object) contract is intentionally unsupported, so calling remove(Object) or removeIf/iterator().remove() throws UnsupportedOperationException. This is by design: the phrase table has no API to delete a single phrase.

Solutions

  1. Use phraseTable.clear() to remove all phrases, then re-add only the phrases you want to keep
  2. Rebuild the collection: create a new PhraseTable, add every phrase except the ones to remove, and swap it in
  3. Keep a separate HashSet<String> of phrases if per-element removal is required, and sync it with add() only

Example fix

// before
phraseStrings.remove("foo"); // throws UnsupportedOperationException
// after
Set<String> kept = new HashSet<>(phraseStrings);
kept.remove("foo");
PhraseTable newTable = new PhraseTable();
for (String p : kept) newTable.addPhrase(p);
Defensive patterns

Strategy: try-catch

Validate before calling

if (phraseStrings instanceof PhraseTable.PhraseStringCollection) {
  throw new IllegalStateException("Use clear()+re-add or a separate Set for removal");
}

Type guard

boolean supportsRemoval(Collection<?> c) {
  return !(c instanceof PhraseTable.PhraseStringCollection);
}

Try / catch

try {
  phraseStrings.remove(phrase);
} catch (UnsupportedOperationException e) {
  // rebuild: clear() and re-add filtered phrases, or use a HashSet copy
}

Prevention

When it happens

Trigger: Calling collection.remove("some phrase") on the Set returned by PhraseTable.getStringCollection()/asCollection-style accessors, or any bulk operation (removeAll, removeIf, iterator.remove) that internally delegates to remove.

Common situations: Developers treating the phrase collection like a normal mutable Set (e.g. pruning phrases after loading a list from a file); refactoring code that previously used a HashSet<String> of phrases into a PhraseTable-backed collection.

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/8468fbe42ea51b70. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java:1159

//          return in.getText();
//        }
//      });
    }

    public Object[] toArray() {
      throw new UnsupportedOperationException("toArray is not supported for PhraseTable.PhraseStringCollection");
    }

    public <T> T[] toArray(T[] a) {
      throw new UnsupportedOperationException("toArray is not supported for PhraseTable.PhraseStringCollection");
    }

    public boolean add(String s) {
      return phraseTable.addPhrase(s);
    }

    public boolean remove(Object o) {
      throw new UnsupportedOperationException("Remove is not supported for PhraseTable.PhraseStringCollection");
    }

    public boolean containsAll(Collection<?> c) {
      for (Object o:c) {
        if (!contains(o)) {
          return false;
        }
      }
      return true;
    }

    public boolean addAll(Collection<? extends String> c) {
      boolean modified = false;
      for (String s:c) {
        if (add(s)) {
          modified = true;
        }
      }

View on GitHub (pinned to 1b7edd19c4)