{"record":{"id":"d986aa893d157c5b","repo":"stanfordnlp/CoreNLP","slug":"cannot-remove-from-values-collection","errorCode":null,"errorMessage":"Cannot remove from values collection","messagePattern":"Cannot remove from values collection","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":2845,"sourceCode":"      }\n\n      public Collection<Double> values() {\n        return new AbstractCollection<Double>() {\n          @Override\n          public Iterator<Double> iterator() {\n            return new Iterator<Double>() {\n              final Iterator<N> it = map.values().iterator();\n\n              public boolean hasNext() {\n                return it.hasNext();\n              }\n\n              public Double next() {\n                return it.next().doubleValue();\n              }\n\n              public void remove() {\n                throw new UnsupportedOperationException(\"Cannot remove from values collection\");\n              }\n            };\n          }\n\n          @Override\n          public int size() {\n            return map.size();\n          }\n        };\n      }\n\n      /**\n       * {@inheritDoc}\n       */\n      public void prettyLog(RedwoodChannels channels, String description) {\n        PrettyLogger.log(channels, description, map);\n      }\n    };","sourceCodeStart":2827,"sourceCodeEnd":2863,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L2827-L2863","documentation":"The counter's values collection view exposes an Iterator whose remove() is intentionally unsupported. Removing through this iterator would desynchronize the wrapper's cached total from the underlying map, so the library throws UnsupportedOperationException instead.","triggerScenarios":"Calling remove() on the Iterator obtained from iterating the counter's values collection (e.g. `counter.values().iterator().remove()` or removing inside a values() loop).","commonSituations":"Trying to drop low-value entries by removing values directly while iterating; works on a HashMap values() view but not this Counter view.","solutions":["Iterate the key set, collect the keys whose values qualify, then remove via counter.remove(key).","Use Counters.retainKeys / threshold filtering utilities.","Construct a filtered copy of the Counter instead of mutating the values view."],"exampleFix":"// before\nIterator<Double> vit = counter.values().iterator();\nwhile (vit.hasNext()) { if (vit.next() < 0.5) vit.remove(); } // throws\n// after\ncounter.keySet().removeIf(k -> counter.getCount(k) < 0.5); // or collect + counter.remove","handlingStrategy":"validation","validationCode":"// treat counter.values() as read-only; remove via keys:\nList<E> victims = counter.keySet().stream()\n    .filter(k -> counter.getCount(k) < threshold)\n    .collect(Collectors.toList());","typeGuard":null,"tryCatchPattern":"try {\n  valuesIterator.remove();\n} catch (UnsupportedOperationException e) {\n  // fall back: remove owning key\n  counter.remove(associatedKey);\n}","preventionTips":["Document/assume values() view is immutable; remove by key instead.","Use removeIf on the key set with a count predicate.","Add code review checks for .remove() on Counter view iterators."],"tags":["unsupported-operation","iterator","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"}