I have a Policies object with two fields: policyNumber and policyStatus. I will have a list of policies, and I need to see if any policy has one of the statuses I provide. If one does, I set a Boolean to yes. If not, I set it to no.
Normally I would use nested for loops like this:
Boolean hasStatus = false;
List<Policy> policies = new List<Policy>();
String[] statuses = String[]{'A1','A3','B6','T1','T6'};
for (Policy policy : policies) {
for (int i=0; i<statuses.length; i++) {
if (policy.policyStatus == statuses[i] {
hasStatus = true;
break;
}
}
}
return hasStatus;
Now I'm extremely week in the areas involving Maps, Sets and Collections and also in Algorithms. What I'm wondering is whether there is something with one of those things that I should be doing with this instead of using the nested for loops and the if block. If there is, could you please provide me some guidance on it? Or is this really how I should be doing this?