{"record":{"id":"6af850924339ce7e","repo":"stanfordnlp/CoreNLP","slug":"iterator-is-not-supported-for-phrasetable-phrasest","errorCode":null,"errorMessage":"iterator is not supported for PhraseTable.PhraseStringCollection","messagePattern":"iterator is not supported for PhraseTable\\.PhraseStringCollection","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java","lineNumber":1137,"sourceCode":"\n    public boolean isEmpty() {\n      return phraseTable.nStrings == 0;\n    }\n\n    public boolean contains(Object o) {\n      if (o instanceof String) {\n        if (useNormalizedLookup) {\n          return (phraseTable.lookupNormalized((String) o) != null);\n        } else {\n          return (phraseTable.lookup((String) o) != null);\n        }\n      } else {\n        return false;\n      }\n    }\n\n    public Iterator<String> iterator() {\n      throw new UnsupportedOperationException(\"iterator is not supported for PhraseTable.PhraseStringCollection\");\n//      return new FunctionApplyingIterator( phraseTable.iterator(), new Function<Phrase,String>() {\n//        @Override\n//        public String apply(Phrase in) {\n//          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);","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/PhraseTable.java#L1119-L1155","documentation":"PhraseTable.PhraseStringCollection is a minimal Collection<String> view over phrases that only supports add, contains, isEmpty, and size. Calling iterator() is deliberately unsupported and throws UnsupportedOperationException because materializing every phrase as a String was not implemented (the real implementation is commented out in the source).","triggerScenarios":"Using the collection in any enhanced-for loop, stream, or Collection-copying API (e.g. new ArrayList<>(phraseStringCollection), CollectionUtils, stream()) that requires iterator().","commonSituations":"Passing PhraseTable.getStringCollection() result to APIs accepting Collection; converting phrases to a list; using streams on it.","solutions":["Iterate the PhraseTable directly (table.iterator()) and call phrase.getText() yourself.","Copy phrases into a real List<String> first, then use streams/enhanced-for on that.","Use getPhrases() or the table iterator to enumerate Phrase objects.","Patch/subclass PhraseStringCollection to implement iterator() with FunctionApplyingIterator if you control the source."],"exampleFix":"// before\nfor (String s : table.getStringCollection()) { ... } // throws\n// after\nList<String> phrases = new ArrayList<>();\nfor (Phrase p : table) { phrases.add(p.getText()); }\nfor (String s : phrases) { ... }","handlingStrategy":"fallback","validationCode":"Collection<String> c = table.getStringCollection();\nboolean safeToForeach = !(c.getClass().getName().endsWith(\"PhraseStringCollection\"));","typeGuard":null,"tryCatchPattern":"try { for (String s : coll) { ... } } catch (UnsupportedOperationException e) { List<String> list = new ArrayList<>(); for (Phrase p : table) list.add(p.getText()); /* use list */ }","preventionTips":["Treat PhraseStringCollection as add/contains/size-only.","Convert to a real List via the table iterator before streaming.","Read the class docs before passing it to Collection-hungry APIs."],"tags":["unsupported-operation","collection","tokensregex"],"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"}