{"record":{"id":"68b708b819fe1d5a","repo":"stanfordnlp/CoreNLP","slug":"cannot-remove-pairs-from-a-merged-iterator","errorCode":null,"errorMessage":"Cannot remove pairs from a merged iterator","messagePattern":"Cannot remove pairs from a merged iterator","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/Iterables.java","lineNumber":418,"sourceCode":"\n          public boolean hasNext() {\n            if (!ready) {\n              pending = nextPair();\n              ready = true;\n            }\n            return pending != null;\n          }\n\n          public Pair<V1, V2> next() {\n            if (!ready && !hasNext()) {\n              throw new IllegalAccessError(\"Called next without hasNext\");\n            }\n            ready = false;\n            return pending;\n          }\n\n          public void remove() {\n            throw new UnsupportedOperationException(\"Cannot remove pairs \" +\n            \"from a merged iterator\");\n          }\n\n          private Pair<V1,V2> nextPair() {\n            V1 nextA = null;\n            V2 nextB = null;\n\n            while (iterA.hasNext() && iterB.hasNext()) {\n              // increment iterators are null\n              if (nextA == null) { nextA = iterA.next(); }\n              if (nextB == null) { nextB = iterB.next(); }\n\n              int cmp = comparator.compare(nextA, nextB);\n              if (cmp < 0) {\n                // iterA too small, increment it next time around\n                nextA = null;\n              } else if (cmp > 0) {\n                // iterB too small, increment it next time around","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/Iterables.java#L400-L436","documentation":"The iterator returned by Iterables.merge (which pairs values from two maps in key order) does not implement removal; remove() always throws UnsupportedOperationException. This is a deliberate design because removing from one of two merged sources while keeping the pairing coherent is not supported.","triggerScenarios":"Calling iterator.remove() on the iterator obtained from a merged Iterable view, e.g. inside a for-each loop using the remove idiom or from wrapper utilities that call remove().","commonSituations":"Trying to delete entries while iterating two synchronized maps, passing the merged iterable to a utility that removes as it processes.","solutions":["Collect keys to remove into a temporary list, then remove them from the underlying map(s) after iteration","Iterate the individual underlying maps directly when removal is needed","Wrap in a new ArrayList or use Iterator over a single map's entrySet","Avoid passing merged iterables to removal-performing utilities"],"exampleFix":"// before\nfor (Pair<K,V> p : Iterables.merge(m1, m2)) {\n  it.remove(); // throws\n}\n// after\nList<K> toRemove = new ArrayList<>();\nfor (Pair<K,V> p : Iterables.merge(m1, m2)) {\n  if (shouldRemove(p)) toRemove.add(p.first);\n}\ntoRemove.forEach(k -> { m1.remove(k); m2.remove(k); });","handlingStrategy":"try-catch","validationCode":"// removal is never supported here; do not call remove()\n// instead collect and remove after iteration\nList<K> toRemove = new ArrayList<>();\nfor (Pair<K,V> p : merged) { if (cond(p)) toRemove.add(p.first); }","typeGuard":null,"tryCatchPattern":"try {\n  it.remove();\n} catch (UnsupportedOperationException e) {\n  // fall back: collect targets and remove from underlying maps afterwards\n}","preventionTips":["Never call remove() on merged iterators","Collect keys during iteration, delete afterwards","Iterate a single underlying map when removal is required","Document that merged views are read-only"],"tags":["iterator","unsupported-operation","java-collections"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}