Skip to content Skip to sidebar Skip to footer

Compare Multiple Boolean Values

i have three boolean value returning from the method , i want to check condition like this :First It will check all the three boolean value Scenario 1: if locationMatch, matchCapa

Solution 1:

Updated your code try this.

publicbooleanmatchFilter(FilterTruck filter) {

    boolean locationMatch = filterMatchesLocation(filter);
    boolean matchCapacity = filterMatchesCapacity(filter);
    boolean filterMatchStatus = filterMatchesStatus(filter);

    return locationMatch && matchCapacity && filterMatchStatus;
}

Solution 2:

Replace your code with below code. Use '&' Operator in your case, because '&' will return true, if expression are satisfying the given conditions, otherwise result will be false

publicbooleanmatchFilter(FilterTruck filter){
            boolean locationMatch = filterMatchesLocation(filter);
            boolean matchCapacity = filterMatchesCapacity(filter);
            boolean filterMatchStatus = filterMatchesStatus(filter);
        return locationMatch && matchCapacity  && filterMatchStatus;
    }

Solution 3:

Remove all the if conditions and return the following from your method

return (locationMatch && matchCapacity && filterMatchStatus);

Solution 4:

Similar to the other answers, but in a shorter form (and it has a single exit point!):

publicbooleanmatchFilter(FilterTruck filter)
{
    boolean locationMatch = filterMatchesLocation(filter);
    boolean matchCapacity = filterMatchesCapacity(filter);
    boolean filterMatchStatus = filterMatchesStatus(filter);
    return (locationMatch && matchCapacity && filterMatchStatus)
}

Solution 5:

publicbooleanmatchFilter(FilterTruck filter){

        boolean locationMatch = filterMatchesLocation(filter);
        boolean matchCapacity = filterMatchesCapacity(filter);
        boolean filterMatchStatus = filterMatchesStatus(filter);

        return locationMatch && matchCapacity && filterMatchStatus;
    }

code minimized. Thank you to @Deepak . Basically you are saying here that only returns true if all 3 variables are true. if one is false, statement returns false (Boolean algebra)

Post a Comment for "Compare Multiple Boolean Values"