简体   繁体   English

如何简化多个“||”?

[英]How can I simplify multiple “||”?

I want to make the if-condition simplify, I really appreciate your advice.我想让 if 条件简化,非常感谢您的建议。

if (StringUtils.isNotBlank(Constant.A)
            || StringUtils.isNotBlank(Constant.B)
            || StringUtils.isNotBlank(Constant.C)
            || StringUtils.isNotBlank(Constant.D)
            || StringUtils.isNotBlank(Constant.E)) {
        return true;
}

You could return directly:您可以直接返回:

return StringUtils.isNotBlank(Constant.A)
        || StringUtils.isNotBlank(Constant.B)
        || StringUtils.isNotBlank(Constant.C)
        || StringUtils.isNotBlank(Constant.D)
        || StringUtils.isNotBlank(Constant.E);

But besides putting that validations inside a method I don't believe you can simplify more.但除了将验证放在一个方法中之外,我不相信你可以简化更多。

If you want to make your condition shorter (but not faster), you can use a stream for this.如果您想让您的条件更短(但不是更快),您可以为此使用stream

if (Stream.of(Constant.A, Constant.B, Constant.C, Constant.D, Constant.E)
          .anyMatch(StringUtils::isNotBlank)) {
    return true;
}

You Could use StringUtils.isAllBlank()你可以使用StringUtils.isAllBlank()

if(!StringUtils.isAllBlank(Constant.A,Constant.B,.......)){
    return true;
}

If a single constant is Not Blank true is returned.如果单个常量不是空白,则返回 true。

TLDR: this condition cannot be simplified in terms of boolean algebra. TLDR:这个条件不能用 boolean 代数来简化。

In boolean algebra there is a notion of canonical normal forms .在 boolean 代数中有一个规范正态 forms的概念。 These forms can be useful for the simplification of boolean expressions, which is of great importance in the optimization of boolean formulas in general.这些 forms 可用于简化 boolean 表达式,这对于优化 boolean 公式非常重要。 There are, basically, two of them: minterms and maxterms.基本上有两个:minterms 和 maxterms。 They are actually duals, ie they are functionally equivalent and can be transformed one into other while preserving the truth table.它们实际上是对偶的,即它们在功能上是等价的,并且可以在保留真值表的同时将它们转换为另一个。

Let's look at maxterms .让我们看看maxterms For a boolean function of N variables, a sum term in which each of the variables appears once (either in its complemented (negated) or uncomplemented form) is called a maxterm.对于 N 个变量的 boolean function,每个变量出现一次的总和项(以补码(否定)或非补码形式)称为最大项。 Thus, a maxterm is a logical expression of N variables that employs only the complement operator and the disjunction operator.因此,maxterm 是 N 个变量的逻辑表达式,它仅使用补码运算符和析取运算符。

That is exactly your case!这正是你的情况!

You have all the variables – A , B , C , D and E – used only once in you expression and you used only ||您拥有所有变量 - ABCDE - 在您的表达式中只使用过一次,并且您只使用了|| operator (which is actually a disjunction).运算符(实际上是析取)。 So, your expression is already in it's normal form and cannot be simplified in terms of needed computations.因此,您的表达式已经处于正常形式,并且无法根据所需的计算进行简化。

You can make it shorter (and more scalable if the number of variables grows) by employing collections / streams operations like other answers advise, but not simpler.您可以像其他答案建议的那样使用 collections / 流操作,使其更短(如果变量数量增加,则更具可扩展性),但并不简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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