{"record":{"id":"8468fbe42ea51b70","repo":"stanfordnlp/CoreNLP","slug":"remove-is-not-supported-for-phrasetable-phrasestri","errorCode":null,"errorMessage":"Remove is not supported for PhraseTable.PhraseStringCollection","messagePattern":"Remove is not supported for PhraseTable\\.PhraseStringCollection","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java","lineNumber":1159,"sourceCode":"//          return in.getText();\n//        }\n//      });\n    }\n\n    public Object[] toArray() {\n      throw new UnsupportedOperationException(\"toArray is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public <T> T[] toArray(T[] a) {\n      throw new UnsupportedOperationException(\"toArray is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public boolean add(String s) {\n      return phraseTable.addPhrase(s);\n    }\n\n    public boolean remove(Object o) {\n      throw new UnsupportedOperationException(\"Remove is not supported for PhraseTable.PhraseStringCollection\");\n    }\n\n    public boolean containsAll(Collection<?> c) {\n      for (Object o:c) {\n        if (!contains(o)) {\n          return false;\n        }\n      }\n      return true;\n    }\n\n    public boolean addAll(Collection<? extends String> c) {\n      boolean modified = false;\n      for (String s:c) {\n        if (add(s)) {\n          modified = true;\n        }\n      }","sourceCodeStart":1141,"sourceCodeEnd":1177,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java#L1141-L1177","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use phraseTable.clear() to remove all phrases, then re-add only the phrases you want to keep","Rebuild the collection: create a new PhraseTable, add every phrase except the ones to remove, and swap it in","Keep a separate HashSet<String> of phrases if per-element removal is required, and sync it with add() only"],"exampleFix":"// before\nphraseStrings.remove(\"foo\"); // throws UnsupportedOperationException\n// after\nSet<String> kept = new HashSet<>(phraseStrings);\nkept.remove(\"foo\");\nPhraseTable newTable = new PhraseTable();\nfor (String p : kept) newTable.addPhrase(p);","handlingStrategy":"try-catch","validationCode":"if (phraseStrings instanceof PhraseTable.PhraseStringCollection) {\n  throw new IllegalStateException(\"Use clear()+re-add or a separate Set for removal\");\n}","typeGuard":"boolean supportsRemoval(Collection<?> c) {\n  return !(c instanceof PhraseTable.PhraseStringCollection);\n}","tryCatchPattern":"try {\n  phraseStrings.remove(phrase);\n} catch (UnsupportedOperationException e) {\n  // rebuild: clear() and re-add filtered phrases, or use a HashSet copy\n}","preventionTips":["Treat PhraseStringCollection as add-only/clear-only; check supported operations before mutating","Keep phrase data in a HashSet if per-element removal is needed","Use clear() plus re-add for bulk pruning"],"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"}