{"record":{"id":"67a64b8dd0c1e272","repo":"apache/druid","slug":"cannot-retainall-ona-an-integerset","errorCode":null,"errorMessage":"Cannot retainAll ona an IntegerSet","messagePattern":"Cannot retainAll ona an IntegerSet","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/collections/IntegerSet.java","lineNumber":145,"sourceCode":"\n  @Override\n  public boolean addAll(Collection<? extends Integer> c)\n  {\n    boolean setChanged = false;\n    for (Integer i : c) {\n      if (!this.contains(i)) {\n        setChanged = true;\n        this.add(i);\n      }\n    }\n    return setChanged;\n  }\n\n  @Override\n  public boolean retainAll(Collection<?> c)\n  {\n    // Stub\n    throw new UnsupportedOperationException(\"Cannot retainAll ona an IntegerSet\");\n  }\n\n  @Override\n  public boolean removeAll(Collection<?> c)\n  {\n    Iterator<?> it = c.iterator();\n    boolean changed = false;\n    while (it.hasNext()) {\n      Integer val = (Integer) it.next();\n      changed = remove(val) || changed;\n    }\n    return changed;\n  }\n\n  @Override\n  public void clear()\n  {\n    mutableBitmap.clear();","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/collections/IntegerSet.java#L127-L163","documentation":"retainAll is intentionally unimplemented in IntegerSet; it is a stub that always throws UnsupportedOperationException. The message contains a typo ('ona an') but the semantics are simply that intersection-based removal is not supported by this class.","triggerScenarios":"Any call to retainAll(collection) on an IntegerSet, e.g. keeping only the elements present in another collection.","commonSituations":"Code written generically against the Set interface (or refactored to use IntegerSet where a HashSet was before) that relies on retainAll for set intersection.","solutions":["Implement intersection manually: iterate the set and remove elements not in the other collection via iterator.remove() with contains() checks — note removeAll IS supported","Copy to a HashSet<Integer>, perform retainAll there, then clear() and addAll() back into the IntegerSet","Build a new IntegerSet containing only elements that pass contains() on the other collection","If retainAll is core to your logic, use HashSet<Integer> instead of IntegerSet"],"exampleFix":"// before\nintegerSet.retainAll(keep); // UnsupportedOperationException\n// after\nintegerSet.removeIf(v -> !keep.contains(v)); // removeIf uses remove(), which is supported","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { set.retainAll(keep); } catch (UnsupportedOperationException e) { set.removeIf(v -> !keep.contains(v)); }","preventionTips":["Never rely on optional Set operations on IntegerSet","Use removeIf (backed by the supported remove()) for intersection","Keep IntegerSet usage narrow; do full set algebra in HashSet"],"tags":["java","set","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}