I have three Integer
variables, where I am not allowed to change to primitive int
and I need to check if at least one of them have a value greater than 0. Is there a shorter / cleaner way to rewrite my code below:
Integer foo = // null or some value
Integer bar = // null or some value
Integer baz = // null or some value
boolean atLeastOnePositive = (foo != null && foo > 0) || (bar != null && bar > 0) || (baz != null && baz > 0)
return atLeastOnePositive;
To check if multiple variables are not null in JavaScript, use the && (and) operator to chain multiple conditions. When used with boolean values the && operator only returns true if both conditions are met. Copied!
if(id == null) is the best method.
You can use Stream
and do like this:
boolean atLeastOnePositive = Stream.of(foo, bar, baz)
.anyMatch(value -> value != null && value > 0);
I guess a varargs method would be the cleanest way:
public static boolean atLeastOnePositive(Integer...integers)
{
for(Integer integer : integers)
{
if(integer != null && integer > 0) return true;
}
return false;
}
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