I have this method that checks the username and password of a user before login. Now my for loop checks only the first item, it finds that the first condition, u.getRole()
Well it makes sense, since if you succeed you return and if you don't, you break, so it breaks:
for (User u: list) {
System.out.println("Before if user is: " + u);
if (u.getRole().equalsIgnoreCase("recruiter")) {
// code which returns at the end
return u;
}
break;
}
The break;
statement is executed whenever the condition isn't met (otherwise you would return
before you reach it), which is why you always check only the first item.
If you want to check all the items, simply remove the break;
statement from your loop.