Given Collection<Object> foo
, I usually do this check before iterating over it:
if (foo != null && !foo.isEmpty()) {
for (Object f : foo) {
// ...
}
}
I'm just wondering, is this considered best practice? Shouldn't the isEmpty()
check be redundant, since for()
will just just ignore an empty collection without throwing errors?
How about the null
check -- is that neccessary or is there a way to make for()
simply ignore null
Collections? (Without try-catching the for-loop)