I have the following logic to remove inactive users in the system, since we cannot remove a row while iterating on the list. Is there a better way to handle this?
List<User> users = new ArrayList<User>();
List<User> removeUsers = new ArrayList<User>();
for (User user : users) {
if (!user.isActive()) {
removeUsers.add(user);
}
}
users.removeAll(removeUsers);
That’s a perfectly fine way to do it IMHO.
Other ways that I might do it, use indexing and remove as you loop through in reverse.
Or create a new list of the items to be kept and replace it with the old list.
Can’t think of any others at the moment.