stanfordnlp/CoreNLP · error · UnsupportedOperationException

retainAll is not supported for…

Error message

retainAll is not supported for PhraseTable.PhraseStringCollection

What it means

PhraseTable.PhraseStringCollection does not implement retainAll(Collection<?>); calling it throws UnsupportedOperationException. Retaining an arbitrary subset would require per-phrase deletion, which PhraseTable does not support. add(), contains(), containsAll() and clear() are supported, but retainAll/removeAll-style subset pruning is not.

Solutions

  1. Copy the collection into a HashSet, call retainAll there, then rebuild a new PhraseTable from the result
  2. Use clear() on the PhraseTable and re-add only the retained phrases
  3. Restructure filtering to happen before phrases are added to the PhraseTable

Example fix

// before
phraseStrings.retainAll(allowed); // throws
// after
Set<String> retained = new HashSet<>(phraseStrings);
retained.retainAll(allowed);
phraseTable.clear();
for (String p : retained) phraseTable.addPhrase(p);
Defensive patterns

Strategy: try-catch

Validate before calling

if (phraseStrings instanceof PhraseTable.PhraseStringCollection) {
  throw new IllegalStateException("retainAll unsupported; copy to HashSet first");
}

Type guard

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

Try / catch

try {
  phraseStrings.retainAll(allowed);
} catch (UnsupportedOperationException e) {
  Set<String> kept = new HashSet<>(phraseStrings);
  kept.retainAll(allowed);
  // rebuild PhraseTable from kept
}

Prevention

When it happens

Trigger: Calling retainAll(...) on the PhraseStringCollection view, or APIs that use retainAll internally (e.g. collection.retainAll(allowedPhrases) to filter a phrase list).

Common situations: Filtering a loaded phrase list down to an allowed subset; code generalized from HashSet<String> to Collection<String> that then calls retainAll.

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

Appendix: source

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

        if (add(s)) {
          modified = true;
        }
      }
      return modified;
    }

    public boolean removeAll(Collection<?> c) {
      boolean modified = false;
      for (Object o:c) {
        if (remove(o)) {
          modified = true;
        }
      }
      return modified;
    }

    public boolean retainAll(Collection<?> c) {
      throw new UnsupportedOperationException("retainAll is not supported for PhraseTable.PhraseStringCollection");
    }

    public void clear() {
      phraseTable.clear();
    }
  }
}

View on GitHub (pinned to 1b7edd19c4)