oldUsers is just a subset of allUsers and all oldUsers are made in-active in the following list. The logic currently works, but I am iterating allUsers just to get the handle of oldUser every time, before I set the active flag to false. Is there a way to pull the appropriate record and make my modifications (oldUsers and allUsers is of type Set<User>)
for (User oldUser : oldUsers) {
for (User user : allUsers) {
if (user.getId().equals(oldUser.getId())) {
user.setActive(false);
}
}
}
If
oldUserscontains copies or fresh instances of users, and you simply want tosetActive(false)on those users inallUsersthat have a matching id, then I suggest you override the .equals (and .hash) methods of User based on the user id. You could then do like this:Another way (perhaps even more elegant) would be to store the users in a
Map<Integer, User>mapping user ids to users. You could then simply do