0

Is there any better way to avoid multiple OR statements in if condition? I have if with more than 8 strings in OR statements.

if(type.equalsIgnoreCase(anotherString1) ||   
   type.equalsIgnoreCase(anotherString2) ||   
   type.equalsIgnoreCase(anotherString3)){
    return true;
}

Looking for better approach to write if with multiple OR statements or how to avoid

João Bravo
  • 206
  • 4
  • 16
ppb
  • 2,299
  • 4
  • 43
  • 75

6 Answers6

7

Simply enough, you can build a Set with the different values to test in lower case (using toLowerCase() if needed).

Then, change your statement to:

if (<set_name>.contains(type.toLowerCase())) 

The interest is also that you add some semantic to your code. Your set will have a name, something functionally suited for your case (allowedUserTypes, elementTypesToProcess or whatever) and can be set somewhere useful for reuse, if relevant.

Steph
  • 1,989
  • 14
  • 18
3

A way to avoid using a chain of if statements or many || operators could be a switch.

To achieve the same outcome using a switch, this would look like :

switch(type){
  case anotherString1:
  case anotherString2:
  case anotherString3:
    return true;
}

notice that each of these cases fall through on eachother, meaning if any of these cases are hit we will return true.

keep in mind this assumes case is already ignored.

more information on switch here: https://www.geeksforgeeks.org/string-in-switch-case-in-java/

JamieT
  • 1,177
  • 1
  • 9
  • 19
  • It is worth noting that when using variables as cases in switch statements, they have to be non-mutable. [They must be constants](https://www.xyzws.com/javafaq/can-i-use-variables-in-case-clauses-in-the-switch-statement/113). – cavpollo Jun 10 '19 at 21:38
1

Supose to manage to get all your otherString1, otherString2, and otherString3 on a list called List<String> otherStrings, then you could take advantage of the contains() method. But this wouldn't solve all your problems, as you want to ignore the case.

So, let's use streams, as this other SO answer suggests:

List<String> otherStrings = ...;

boolean containsType = otherStrings.stream()
    .anyMatch(otherString-> otherString.equalsIgnoreCase(type));

This has the advantage of allowing you to include any number of otherStrings in the List without having to change the implementation.

cavpollo
  • 4,071
  • 2
  • 40
  • 64
0

If all your conditions try to match for a given string, you could have a list of strings and use contains (https://docs.oracle.com/javase/8/docs/api/java/util/List.html#contains-java.lang.Object-)

List<String> list = Arrays.asList("type1", "type2",......);

if (type != null) {
    if (list.contains(type.toLowerCase())) {
        return true;
    }
}
0

Using streams you could do something like:

return  Stream.of(anotherString1,anotherString2,anotherString3)
              .anyMatch(e->e.equalsIgnoreCase(type));

which allows you to omit even the if statement.

Eritrean
  • 15,851
  • 3
  • 22
  • 28
  • 2
    Beware that omitting the if statement actually changes the semantics. In the OP example, you don't know what happens if the condition is not met. Maybe other tests are performed and the method result might still be true! – Steph Jun 10 '19 at 21:59
0

Since no state is involved, recommend you create a static method as follows.

   public static boolean anyEqual(String arg, String... strs) {
      return Arrays.stream(strs).map(String::toLowerCase).collect(
            Collectors.toSet()).contains(arg.toLowerCase());
   }

Then you can call the method in either fashion below:

      String[] strs = { "string1", "string2", "string3"
      };

      System.out.println(anyEqual("string2", strs));
      System.out.println(anyEqual("test", "test1", "test2", "test3"));
WJS
  • 36,363
  • 4
  • 24
  • 39