简体   繁体   中英

better way to avoid multiple OR statements in java if condition

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

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.

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/

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 otherString s in the List without having to change the implementation.

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;
    }
}

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.

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"));

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