{"record":{"id":"05c2b5b8f562cdf9","repo":"apache/hadoop","slug":"retainall-is-not-supported","errorCode":null,"errorMessage":"retainAll is not supported.","messagePattern":"retainAll 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/LightWeightHashSet.java","lineNumber":648,"sourceCode":"        return false;\n      }\n    }\n    return true;\n  }\n\n  @Override\n  public boolean removeAll(Collection<?> c) {\n    boolean changed = false;\n    Iterator<?> iter = c.iterator();\n    while (iter.hasNext()) {\n      changed |= remove(iter.next());\n    }\n    return changed;\n  }\n\n  @Override\n  public boolean retainAll(Collection<?> c) {\n    throw new UnsupportedOperationException(\"retainAll is not supported.\");\n  }\n}\n","sourceCodeStart":630,"sourceCodeEnd":651,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/LightWeightHashSet.java#L630-L651","documentation":"retainAll is an optional java.util.Set operation that LightWeightHashSet intentionally does not implement - it throws UnsupportedOperationException unconditionally. add/remove/removeAll/contains are all implemented, so code that assumes the full Set interface works until it performs an intersection.","triggerScenarios":"Any call to set.retainAll(collection) on a LightWeightHashSet or LightWeightLinkedSet instance - including from generic library code (CollectionUtils, set-algebra helpers) that only sees the Set interface.","commonSituations":"Computing intersections of tracked objects (e.g., 'keep only live replicas'); utilities that call retainAll internally; polymorphic code previously tested only against HashSet.","solutions":["Implement the intersection manually: iterate with the iterator and it.remove() everything not in the keep-collection (iterator remove is supported in LightWeightHashSet).","Or build the result without mutating: set.stream().filter(keep::contains).collect(Collectors.toSet()).","Wrap in new HashSet<>(set) when full Set semantics including retainAll are required."],"exampleFix":"// before\nset.retainAll(liveIds); // UnsupportedOperationException\n\n// after - iterator-based removal, supported by LightWeightHashSet\nfor (Iterator<T> it = set.iterator(); it.hasNext(); ) {\n  if (!liveIds.contains(it.next())) it.remove();\n}","handlingStrategy":"fallback","validationCode":"// intersection without retainAll - LightWeightHashSet supports iterator removal\nfor (Iterator<T> it = set.iterator(); it.hasNext(); ) {\n  if (!keep.contains(it.next())) it.remove();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat retainAll as unimplemented on all light-weight Hadoop collections - check instanceof before calling on generic Set references.","Centralize set algebra in helpers that branch on implementation capability.","Unit-test generic collection utilities against these custom Set implementations."],"tags":["unsupported-operation","set","retainall","set-algebra"],"backgroundTag":"unsupported-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}