stanfordnlp/CoreNLP · error · java.lang.UnsupportedOperationException

iterator is not supported for…

Error message

iterator is not supported for PhraseTable.PhraseStringCollection

What it means

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).

Solutions

  1. Iterate the PhraseTable directly (table.iterator()) and call phrase.getText() yourself.
  2. Copy phrases into a real List<String> first, then use streams/enhanced-for on that.
  3. Use getPhrases() or the table iterator to enumerate Phrase objects.
  4. Patch/subclass PhraseStringCollection to implement iterator() with FunctionApplyingIterator if you control the source.

Example fix

// before
for (String s : table.getStringCollection()) { ... } // throws
// after
List<String> phrases = new ArrayList<>();
for (Phrase p : table) { phrases.add(p.getText()); }
for (String s : phrases) { ... }
Defensive patterns

Strategy: fallback

Validate before calling

Collection<String> c = table.getStringCollection();
boolean safeToForeach = !(c.getClass().getName().endsWith("PhraseStringCollection"));

Try / catch

try { for (String s : coll) { ... } } catch (UnsupportedOperationException e) { List<String> list = new ArrayList<>(); for (Phrase p : table) list.add(p.getText()); /* use list */ }

Prevention

When it happens

Trigger: Using the collection in any enhanced-for loop, stream, or Collection-copying API (e.g. new ArrayList<>(phraseStringCollection), CollectionUtils, stream()) that requires iterator().

Common situations: Passing PhraseTable.getStringCollection() result to APIs accepting Collection; converting phrases to a list; using streams on it.

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

Appendix: source

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

    public boolean isEmpty() {
      return phraseTable.nStrings == 0;
    }

    public boolean contains(Object o) {
      if (o instanceof String) {
        if (useNormalizedLookup) {
          return (phraseTable.lookupNormalized((String) o) != null);
        } else {
          return (phraseTable.lookup((String) o) != null);
        }
      } else {
        return false;
      }
    }

    public Iterator<String> iterator() {
      throw new UnsupportedOperationException("iterator is not supported for PhraseTable.PhraseStringCollection");
//      return new FunctionApplyingIterator( phraseTable.iterator(), new Function<Phrase,String>() {
//        @Override
//        public String apply(Phrase in) {
//          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);

View on GitHub (pinned to 1b7edd19c4)