简体   繁体   中英

Multiple boolean values in if else statement is always false in Java

I am stuck to this method because of the if else condition says that

  • Condition usersInSales && usersInPayments is always false
  • Condition usersInSales && usersInLoans is always false
  • Condition usersInPayments && usersInLoans is always false

I tried different condition combinations and added the false values to try resolve it but it didn't help. Please can I have some help? Thanks in advance

private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {

        if (usersInSales) {
            return getUsersInSales(users);
        } else if (usersInPayments) {
            return getUsersInPayments(users);
        } else if (usersInLoans) {
            return getUsersInLoans(users);
        } else if (usersInSales && usersInPayments) {
            return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
        } else if (usersInSales && usersInLoans) {
            return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
        } else if (usersInPayments && usersInLoans) {
            return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
        } else return users;
    }
if (usersInSales) {
        return getUsersInSales(users);
    } else if (usersInPayments) {    ---> You get here when userInSales = false
        return getUsersInPayments(users);
    } else if (usersInLoans) {  --> You get here when usersInSales = false && usersInPayments = false
        return getUsersInLoans(users);
    }  else if () {  --> You get here when usersInSales = false && usersInPayments = false && usersInLoans = false. No use in comparing what you compare here. It will be always false as it reports to
     }

Hope you now can figure your way out

You could try to check first the most specific constraints and then in the end move to more general constraints.

if (usersInSales && usersInPayments) {
        return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
    } else if (usersInSales && usersInLoans) {
        return Stream.concat(getUsersInSales(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
    } else if (usersInPayments && usersInLoans) {
        return Stream.concat(getUsersInPayments(users).stream(), getUsersInLoans(users).stream()).distinct().collect(Collectors.toList());
    } else if (usersInSales) {
        return getUsersInSales(users);
    } else if (usersInPayments) {
        return getUsersInPayments(users);
    } else if (usersInLoans) {
        return getUsersInLoans(users);
    } else return users;

Checking your workflow however IMO your method would have more sense without all those if, else checks. Just this could be enough

 private List<UserResource> usersFilteredByDepartment(List<UserResource> users, boolean usersInSales, boolean usersInPayments, boolean usersInLoans) {
    
     return Stream.concat(
usersInPayments? getUsersInPayments(users).stream(): Stream.empty(),
usersInLoans? getUsersInLoans(users).stream(): Stream.empty(), 
usersInSales? getUsersInSales(users).stream(): Stream.empty()
).distinct().collect(Collectors.toList());
    
}

You have two solutions.

  1. Reorder your conditions, as others have shown. In your code by time you hit the && statements you have already dealt with the cases when one half is true. The && (two clauses) is more restrictive than a single clause.

  2. Alternatively, put the double clauses inside the previous ifs.

     if (usersInSales) { return getUsersInSales(users); } else if (usersInPayments) { return getUsersInPayments(users); } else if (usersInLoans) { return getUsersInLoans(users); } else if (usersInSales && usersInPayments) { return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList()); } else if (usersInSales && usersInLoans) {...

becomes

    if (usersInSales) {
        if (usersInPayments) { // Nested if is like && operator.
            return Stream.concat(getUsersInSales(users).stream(), getUsersInPayments(users).stream()).distinct().collect(Collectors.toList());
        } else {
            return getUsersInSales(users);
        }
    } else if (usersInPayments) { ...

This is a little more efficient and I think generally preferable.

Consider your logic: you first check usersInSales , and return if it's true. Then you check usersInPayments and return if it's true. So when you then check usersInSales && usersInPayments , both will be false because you already handled the cases when either of them are true.

You need to change your logic so you handle the conditions additive instead of exclusively.

your last 3 conditions will never run. because one of your first 3 conditions will already be true. To fix this you need to reverse the order of your conditions, try this:

if (usersInSales && usersInPayments) {
    } else if (usersInSales && usersInLoans) {
    } else if (usersInPayments && usersInLoans) {
    } else if (usersInSales ) {
    } else if ( usersInLoans) {
    } else if (usersInLoans) {
    } else return users;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM