{"record":{"id":"8a5f756711068a81","repo":"apache/hadoop","slug":"remove-is-not-supported","errorCode":null,"errorMessage":"Remove is not supported.","messagePattern":"Remove is not supported\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightLinkedSet.java","lineNumber":267,"sourceCode":"\n    @Override\n    public T next() {\n      if (modification != startModification) {\n        throw new ConcurrentModificationException(\"modification=\"\n            + modification + \" != startModification = \" + startModification);\n      }\n      if (next == null) {\n        throw new NoSuchElementException();\n      }\n      final T e = next.element;\n      // find the next element\n      next = next.after;\n      return e;\n    }\n\n    @Override\n    public void remove() {\n      throw new UnsupportedOperationException(\"Remove is not supported.\");\n    }\n  }\n\n  /**\n   * Clear the set. Resize it to the original capacity.\n   */\n  @Override\n  public void clear() {\n    super.clear();\n    this.head = null;\n    this.tail = null;\n    this.resetBookmark();\n  }\n\n  /**\n   * Returns a new iterator starting at the bookmarked element.\n   *\n   * @return the iterator to the bookmarked element.","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightLinkedSet.java#L249-L285","documentation":"Unlike LightWeightHashSet's iterator (which implements remove), LightWeightLinkedSet's iterator.remove() unconditionally throws UnsupportedOperationException. Removing through the iterator would need to unlink nodes from both the hash chain and the insertion-order doubly-linked list, which the implementation deliberately does not support.","triggerScenarios":"Calling it.remove() inside any loop over a LightWeightLinkedSet - including code ported from LightWeightHashSet or HashSet where iterator removal worked.","commonSituations":"Shared cleanup utilities written against Iterator.remove(); refactors between the two light-weight collections; generic filtering code that assumes Iterator's optional remove() exists.","solutions":["Collect elements to remove into a temp list during iteration, then call set.removeAll(temp) (or set.remove(e)) afterwards.","Iterate a copy and remove from the original (see 3253 example).","If iterator-level removal is a hard requirement, use LightWeightHashSet where insertion order does not matter."],"exampleFix":"// before\nfor (Iterator<T> it = set.iterator(); it.hasNext(); ) {\n  if (stale(it.next())) it.remove(); // UnsupportedOperationException\n}\n\n// after - defer removals\nList<T> doomed = new ArrayList<>();\nfor (T e : set) {\n  if (stale(e)) doomed.add(e);\n}\nset.removeAll(doomed);","handlingStrategy":"fallback","validationCode":"// collect-then-remove is the only safe removal-during-iteration pattern here\nList<T> doomed = new ArrayList<>();\nfor (T e : set) { if (stale(e)) doomed.add(e); }\nset.removeAll(doomed);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Iterator.remove() is optional in the JDK contract - verify support per implementation before relying on it.","Keep removal logic in shared helpers that use collect-then-remove, safe for both light-weight collections.","Use LightWeightHashSet when iterator-level removal is required and order does not matter."],"tags":["unsupported-operation","iterator","remove","linked-set"],"backgroundTag":"unsupported-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}