{"record":{"id":"2835d8096f19bc8f","repo":"apache/hadoop","slug":"there-is-no-current-element-to-remove","errorCode":null,"errorMessage":"There is no current element to remove","messagePattern":"There is no current element to remove","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":351,"sourceCode":"    }\n\n    @Override\n    public E next() {\n      ensureNext();\n      if (next == null) {\n        throw new IllegalStateException(\"There are no more elements\");\n      }\n      cur = next;\n      next = null;\n      return convert(cur);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public void remove() {\n      ensureNext();\n      if (cur == null) {\n        throw new IllegalStateException(\"There is no current element \" +\n            \"to remove\");\n      }\n      LightWeightGSet.this.remove((K)cur);\n      iterModification++;\n      cur = null;\n    }\n\n    public void setTrackModification(boolean trackModification) {\n      this.trackModification = trackModification;\n    }\n  }\n  \n  /**\n   * Let t = percentage of max memory.\n   * Let e = round(log_2 t).\n   * Then, we choose capacity = 2^e/(size of reference),\n   * unless it is outside the close interval [1, 2^30].\n   *","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L333-L369","documentation":"Iterator remove() throws IllegalStateException(\"There is no current element to remove\") when cur == null: either remove() was called before any next(), or remove() was called twice without an intervening next() (a successful remove clears cur).","triggerScenarios":"Calling it.remove() before the first it.next(); or two consecutive it.remove() calls with no next() between them.","commonSituations":"Filter loops trying to remove the 'current' element before advancing; copy-paste of remove() into a loop containing a conditional next(); retry logic that re-invokes remove() after it already succeeded.","solutions":["Call next() first, and remove only the element it just returned.","Ensure at most one remove() per next().","In retry logic, track whether the previous remove() succeeded instead of calling it again."],"exampleFix":"// before\nwhile (it.hasNext()) {\n  it.remove(); // no next() first -> IllegalStateException\n}\n\n// after\nwhile (it.hasNext()) {\n  E e = it.next();\n  if (shouldDrop(e)) {\n    it.remove(); // exactly one remove per next()\n  }\n}","handlingStrategy":"validation","validationCode":"E e = it.next();\nif (shouldDrop(e)) {\n  it.remove(); // remove() only immediately after a successful next()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Enforce the one-remove-per-next discipline in review checklists.","Never call remove() as the first operation on a fresh iterator."],"tags":["collections","iterator","hadoop-common"],"backgroundTag":"invalid-iterator-state","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}