Is there an easy way to code multiple if conditions?

I have the code below in Java. I need to execute all the instructions if.Is have a better approach to the this.In code of each of the instructions, I would then make a database call.

if (!keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("1");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("2");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("3");
}
if (keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("4");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("5");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
    templateOrder.add("6");
}
+4
source share
5 answers

You can create a bitmask and enable it:

public static final int KEY = 1;
public static final int SEGMENT = 2;
public static final int REGION = 4;

int value = 0;

if (keyAccntId.equalsIgnoreCase("-1")) {
    value += KEY;
}
if (segmentId.equalsIgnoreCase("-1")) {
    value += SEGMENT;
}
if (regionId.equalsIgnoreCase("-1")) {
    value += REGION;
}

This gives you 8 possible values ​​that you can include:

switch(value) {
case 0:
    // All false
    break;
case KEY:
    // only keyAccntId is true
    break;
case REGION:
    // only segmentId is true
    break;
case KEY + REGION:
    // segmentId and keyAccntId are true
    break;
// So on
}

change

Constants added to improve readability.

+16
source

A shorter version of @ njzk2's answer using Java 7.

public static final int KEY = 0b001, SEGMENT = 0b010, REGION = 0b100;

int comb = (keyAccntId.equals("-1") ? KEY : 0) +
        (segmentId.equals("-1") ? SEGMENT : 0) +
        (regionId.equals("-1") ? REGION : 0);
switch(comb) {
     case 0:       /* none are true. */ break;
     case REGION:  /* only regionId is -1 */ break;
     // more combinations.
     case KEY + SEGMENT + REGION: /* all are -1 */ break;
}
+7
source

"If", No "Switch". 3 :

 int val = keyAccntId.equals("-1")? 3:0;
 val += ((segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0);
 templateOrder.add(val+1+"");

.

, , :

public int getTemplateOrder(int keyAccntId, int segmentId, int regionId){
  int val = keyAccntId.equals("-1")? 3:0;
  return val + (segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0) + 1;
}

//Usage
templateOrder.add(getTemplateOrder(keyAccntId, segmentId, regionId)+"");

.

:

   int acc = keyAccntId.equals("-1")? 3:0;
   int seg = segmentId.equals("-1")? 1:0;
   int reg = regionId.equals("-1")? 1:0;
   int val = acc;

   //Construct the value currently being used on each If statement.
   val += seg + reg;

   //You've got the value, so instead of If/switch, just set it.
   templateOrder.add(val+1+"");

, .

.

+2

, - ...

boolean keyTF = keyAccntId.equalsIgnoreCase("-1"), 
segmentTF = segmentId.equalsIgnoreCase("-1"), 
regionTF = regionId.equalsIgnoreCase("-1");

if (!keyTF  && !segmentTF  && !regionTF ) {
    templateOrder.add("1");
}
if (!keyTF  && segmentTF  && !regionTF ) {
    templateOrder.add("2");
}
if (!keyTF  && segmentTF  && regionTF ) {
    templateOrder.add("3");
}
if (keyTF  && !segmentTF  && !regionTF ) {
    templateOrder.add("4");
}
if (keyTF  && segmentTF  && !regionTF ) {
    templateOrder.add("5");
}
if (keyTF  && segmentTF  && regionTF ) {
    templateOrder.add("6");
}

, , . 3 ,

FFF
FTF
FTT
TFF
TTF
TTT

TFT:)

0

add.

, add OP-, if ... TRUE, FALSE.

:

keyAccntId.equalsIgnoreCase("-1") |       F       |       T       |
segmentId.equalsIgnoreCase("-1")  |   F   |   T   |   F   |   T   |
regionId.equalsIgnoreCase("-1")   | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
templateOrder.add("1");           | y | - | - | - | - | - | - | - |
templateOrder.add("2");           | - | - | y | - | - | - | - | - |
templateOrder.add("3");           | - | - | - | y | - | - | - | - |
templateOrder.add("4");           | - | - | - | - | y | - | - | - |
templateOrder.add("5");           | - | - | - | - | - | - | y | - |
templateOrder.add("6");           | - | - | - | - | - | - | - | y |
----------------------------------+---+---+---+---+---+---+---+---+

If the resulting values ​​were β€œaligned” with the conditions better, then you could reduce the number of times the comparisons were performed.

int n = 0;
if (keyAccntId.equalsIgnoreCase("-1") {
  n += 4;
} 
if (segmentId.equalsIgnoreCase("-1") {
  n += 2;
}  
if (regionId.equalsIgnoreCase("-1") {
  n += 1;
}
templateOrder.add(String.valueOf(n));

NOTE. the above code is NOT equivalent to the OP code. To make it execute the equivalent, we need to add a conditional test, so that the method is addnot called when n is 1 or 5, and to convert the value nto the specified value of the string argument

n    arg
---  ----
0    "1"
1    - 
2    "2"
3    "3"
4    "4"
5    -
6    "5"
7    "6"

The logical table will look something like this:

keyAccntId.equalsIgnoreCase("-1") |       F       |       T       |
segmentId.equalsIgnoreCase("-1")  |   F   |   T   |   F   |   T   |
regionId.equalsIgnoreCase("-1")   | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
n += 4;                           | - | - | - | - | y | y | y | y |
n += 2;                           | - | - | y | y | - | - | y | y |
n += 1;                           | - | y | - | y | - | y | - | y |
templateOrder.add(arg);           | y | - | y | y | y | - | y | y |
----------------------------------+---+---+---+---+---+---+---+---+
0
source

Source: https://habr.com/ru/post/1543236/


All Articles