首页 > 解决方案 > 如何结合这两个地图条件

问题描述

有没有办法将这两个条件组合在一个变量中?

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
}

我需要结合notNullMappingPresent1nullMappingPresent1结合notNullMappingPresent2and nullMappingPresent2。我们可以组合创建 2 个而不是创建 4 个布尔变量吗?

标签: javaconditional-statements

解决方案


看来,应该提取空检查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
        }
    }
}

推荐阅读