简体   繁体   English

如何结合这两个地图条件

[英]how to combine these two map conditions

Is there any way to combine these two conditions in one variable?有没有办法将这两个条件组合在一个变量中?

boolean notNullMappingPresent1 = !isNullOrEmpty(map1) && (isNotNullOrEmpty(map1.get("Value"));
boolean nullMappingPresent1 = (!isNullOrEmpty(map1) && isNullOrEmpty(map1.get("Value")));

boolean notNullMappingPresent2 = !isNullOrEmpty(map2) && (isNotNullOrEmpty(map2.get("Value"));
boolean nullMappingPresent2 = (!isNullOrEmpty(map2) && isNullOrEmpty(map2.get("Value")));


if(notNullMappingPresent1){
    //lines of code
}
if(notNullMappingPresent2){
    //lines of code
}
if(nullMappingPresent1 && nullMappingPresent2){
    //lines of code
}

I need to combine notNullMappingPresent1 and nullMappingPresent1 also combine notNullMappingPresent2 and nullMappingPresent2 .我需要结合notNullMappingPresent1nullMappingPresent1还结合notNullMappingPresent2nullMappingPresent2 Instead of creating 4 boolean variables can we combine to create 2?我们可以组合创建 2 个,而不是创建 4 个布尔变量吗?

It seems, that the null check for map1 and map2 should be extracted, then the two expressions could be used:看来,应该提取map1map2的空检查,然后可以使用这两个表达式:

if (!isNullOrEmpty(map1) && !isNullOrEmpty(map2)) {
    boolean nullMapping1 = isNullOrEmpty(map1.get("Value"));
    boolean nullMapping2 = isNullOrEmpty(map2.get("Value"));

    if (nullMapping1 && nullMapping2) {
       // lines of code
    } else {
        if (!nullMapping1) {
            // lines of code
        }
        if (!nullMapping2) {
            // lines of code
        }
    }
}

Let's see this code:让我们看看这段代码:

boolean notNullMappingPresent1 = !isNullOrEmpty(map1) && (isNotNullOrEmpty(map1.get("Value"));
boolean nullMappingPresent1 = (!isNullOrEmpty(map1) && isNullOrEmpty(map1.get("Value")));

!isNullOrEmpty(map1) is equivalent to !isNullOrEmpty(map1) !isNullOrEmpty(map1)等价于!isNullOrEmpty(map1)

isNotNullOrEmpty(map1.get("Value") is the exact opposite of isNullOrEmpty(map1.get("Value")) isNotNullOrEmpty(map1.get("Value")isNullOrEmpty(map1.get("Value"))正好相反

So, in order to simplify this, let's rephrase this in a more sensible manner:因此,为了简化这一点,让我们以更明智的方式重新表述:

boolean map1NotEmpty = !isNullOrEmpty(map1); boolean map1NotEmpty = !isNullOrEmpty(map1); boolean map2NotEmpty = !isNullOrEmpty(map2); boolean map2NotEmpty = !isNullOrEmpty(map2); boolean map1Value = isNotNullOrEmpty(map1.get("Value")); boolean map1Value = isNotNullOrEmpty(map1.get("Value")); boolean map2Value = isNotNullOrEmpty(map2.get("Value")); boolean map2Value = isNotNullOrEmpty(map2.get("Value"));

So, your logic would look like this:所以,你的逻辑看起来像这样:

if (map1NotEmpty || map2NotEmpty) {
    if (map1NotEmpty && map1Value) {
        //...
    } else if (map1NotEmpty && map2Value) {
        //...
    } else if (map1NotEmpty && map2NotEmpty)
}

I still use four variables, yes, because there are four things to know:我仍然使用四个变量,是的,因为有四件事要知道:

  • is map1 empty? map1 是空的吗?
  • is map1 containing value? map1 包含值吗?
  • is map2 empty? map2 是空的吗?
  • is map2 containing value? map2 包含值吗?

Technically I could combine all this into numeric code, containing a single value, but that would be so hacky that there is no real-life use of it.从技术上讲,我可以将所有这些组合成包含单个值的数字代码,但这太笨拙了,以至于在现实生活中没有使用它。

You can combine this two variables like this :您可以像这样组合这两个变量:

if(!isNullOrEmpty(map1) && isNotNullOrEmpty(map1.get("Value")) && isNullOrEmpty(map1.get("Value"))) {
    // Line of code
}

if(!isNullOrEmpty(map2) && isNotNullOrEmpty(map2.get("Value")) && isNullOrEmpty(map2.get("Value"))) {
    // Line of Code
}

Note: Above condition can't match all the time because last two condition functions in first condition is exactly opposite of each other so.注意:上述条件不能一直匹配,因为第一个条件中的最后两个条件函数彼此完全相反。

I think one option is use a int instead of boolean so that we can have 3 values and can check all the conditions:我认为一种选择是使用 int 而不是 boolean 这样我们可以有 3 个值并可以检查所有条件:

int mapCode1 = !isNullOrEmpty(map1)?(isNotNullOrEmpty(map1.get("Value1")))?1:0:-1;
int mapCode2 = !isNullOrEmpty(map2)?(isNotNullOrEmpty(map2.get("Value2")))?1:0:-1;


if(mapCode1==0 && mapCode2==0){
        System.out.println("Values of map1 and map2 are null");
    }
if(mapCode1==1){
    System.out.println("Values of map1 are not null");
}
if(mapCode2==1){
    System.out.println("Values of map2 are not null");
}

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

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