About this question
It seems to be a very common thing to have to tell whether some list or set contains at least one object matching a given condition, yet my prior searching and reading have never found a satisfactory best practice or design pattern to speak of. In the situations I am thinking of, merely using "contains" is not sufficient, as the test we want to perform is not strictly based on the equals method of the class in question.
Some things I have seen:
// for-each with break
private boolean doFoosHaveGoodQuality(Listcandidates) { boolean foundIt = false;
for (Foo item: candidates) {
if (<< item>>) {
fountIt = true;
break;
}
}
return foundIt;
}
// while-do with index counter
private boolean doFoosHaveGoodQuality(Listcandidates) { boolean foundIt = false;
int index = 0;
while(!fountIt && index < candidates> Foo item = candidates.get(index++);
if (<< item>>) {
fountIt = true;
}
}
return foundIt;
}
Both of these get the job done and quit as soon as a success is found, possibly saving some time if we are lucky enough to have the desired item early in the list. However, both require looping the entire list in order to get a false result
Another technique I've seen in some circumstances is that at the same time the original List
I can't put a finger on it. Each idea works, yet each feels just a little hacky and inefficient.