Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Medium · Pro

Debug: UnsupportedOperationException when cleaning a list

JavaCollectionsTrickySenior

Removing inactive IDs crashes at runtime.

Java
List<String> all = repository.findAllIds();
List<String> view = all.subList(0, Math.min(50, all.size()));
for (String id : view) {
    if (isInactive(id)) {
        view.remove(id);
    }
}
Model answerPreview

Root cause

  • subList view does not support structural modification via its own iterator/remove.