asha ganapathy
asha ganapathy

Reputation: 113

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. Instead of creating 4 boolean variables can we combine to create 2?

Upvotes: 0

Views: 159

Answers (4)

asha ganapathy
asha ganapathy

Reputation: 113

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

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76574

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)

isNotNullOrEmpty(map1.get("Value") is the exact opposite of isNullOrEmpty(map1.get("Value"))

So, in order to simplify this, let's rephrase this in a more sensible manner:

boolean map1NotEmpty = !isNullOrEmpty(map1); boolean map2NotEmpty = !isNullOrEmpty(map2); boolean map1Value = isNotNullOrEmpty(map1.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?
  • is map1 containing value?
  • is map2 empty?
  • is map2 containing value?

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.

Upvotes: 1

Nowhere Man
Nowhere Man

Reputation: 19545

It seems, that the null check for map1 and map2 should be extracted, then the two expressions could be used:

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

Upvotes: 1

Mayur Kukadiya
Mayur Kukadiya

Reputation: 2747

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.

Upvotes: 0

Related Questions