{"record":{"id":"acc9f0014e2b4427","repo":"stanfordnlp/CoreNLP","slug":"retainall-is-not-supported-for-phrasetable-phrases","errorCode":null,"errorMessage":"retainAll is not supported for PhraseTable.PhraseStringCollection","messagePattern":"retainAll is not supported for PhraseTable\\.PhraseStringCollection","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java","lineNumber":1192,"sourceCode":"        if (add(s)) {\n          modified = true;\n        }\n      }\n      return modified;\n    }\n\n    public boolean removeAll(Collection<?> c) {\n      boolean modified = false;\n      for (Object o:c) {\n        if (remove(o)) {\n          modified = true;\n        }\n      }\n      return modified;\n    }\n\n    public boolean retainAll(Collection<?> c) {\n      throw new UnsupportedOperationException(\"retainAll is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public void clear() {\n      phraseTable.clear();\n    }\n  }\n}\n","sourceCodeStart":1174,"sourceCodeEnd":1200,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java#L1174-L1200","documentation":"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.","triggerScenarios":"Calling retainAll(...) on the PhraseStringCollection view, or APIs that use retainAll internally (e.g. collection.retainAll(allowedPhrases) to filter a phrase list).","commonSituations":"Filtering a loaded phrase list down to an allowed subset; code generalized from HashSet<String> to Collection<String> that then calls retainAll.","solutions":["Copy the collection into a HashSet, call retainAll there, then rebuild a new PhraseTable from the result","Use clear() on the PhraseTable and re-add only the retained phrases","Restructure filtering to happen before phrases are added to the PhraseTable"],"exampleFix":"// before\nphraseStrings.retainAll(allowed); // throws\n// after\nSet<String> retained = new HashSet<>(phraseStrings);\nretained.retainAll(allowed);\nphraseTable.clear();\nfor (String p : retained) phraseTable.addPhrase(p);","handlingStrategy":"try-catch","validationCode":"if (phraseStrings instanceof PhraseTable.PhraseStringCollection) {\n  throw new IllegalStateException(\"retainAll unsupported; copy to HashSet first\");\n}","typeGuard":"boolean supportsRetainAll(Collection<?> c) {\n  return !(c instanceof PhraseTable.PhraseStringCollection);\n}","tryCatchPattern":"try {\n  phraseStrings.retainAll(allowed);\n} catch (UnsupportedOperationException e) {\n  Set<String> kept = new HashSet<>(phraseStrings);\n  kept.retainAll(allowed);\n  // rebuild PhraseTable from kept\n}","preventionTips":["Never call subset operations (retainAll/removeAll) on PhraseTable views","Filter phrases before adding them to the PhraseTable","Copy to a plain Set for set algebra, then rebuild the table"],"tags":["java","unsupported-operation","collection"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}