避免在条件中出现多个OR语句的更好方法

ppb*_*ppb 0 java

有没有更好的办法,以避免重复OR的语句if条件?我有if超过8个字符串的OR语句。

if(type.equalsIgnoreCase(anotherString1) ||   
   type.equalsIgnoreCase(anotherString2) ||   
   type.equalsIgnoreCase(anotherString3)){
    return true;
}
Run Code Online (Sandbox Code Playgroud)

寻找更好的方法来编写if多条OR语句或如何避免

Ste*_*eph 7

简而言之,您可以使用不同的值构建Set以测试小写toLowerCase()如果需要,可以使用)。

然后,将您的声明更改为:

if (<set_name>.contains(type.toLowerCase())) 
Run Code Online (Sandbox Code Playgroud)

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.