Every now and then there is a need to store a boolean value only once (to record that it has changed from false to true or vice versa) in a loop, while executing the loop to the end but not caring anymore about changes in the boolean value. Example:
public static boolean dbDelete(Collection argObjectCollectionToDelete) {
boolean result = true;
for (Object object : argObjectCollectionToDelete) {
boolean dbDelete = dbDelete(object);
if (!dbDelete) {
result = false;
}
}
return result;
}
Is there some way to execute the equivalent of the code
if (!dbDelete) {
result = false;
}
or
if (!dbDelete && !result) {
result = false;
}
in a more elegant way, preferrably in one line?
How about:
result &= dbDelete(object);
This is equivalent to:
result = result & dbDelete(object);
So it will only be true if result
was previously true and dbDelete
returned true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With