简体   繁体   中英

multiple if else if with && condition in JAVA8

I want to convert below code in java 8 best code practice

if(i==0 && j==0) {
    return 1;    
} else if (i==0 && j==1) {
    return 2;
} else if (i==1 && j==0) {
    return 3;
} else if (i==1 && j==1) {
    return 4;
} 

EDIT: OP posted as a comment to the question

if(counterFlag==0 && priorityEnable==0) { 
    return 0; 
} else if (counterFlag==0 && priorityEnable==1) { 
    return 1; 
} else if (counterFlag==1 && priorityEnable==0) { 
    return 2; 
} else { 
    return 3; 
}

The best I can think of for the ORIGINAL problem without being obscure / unreadable is this:

if (i == 0) {
    if (j == 0) {
        System.out.println("print something ");     
    } else if (j == 1) {
         System.out.println("print something ");     
    }
} else if (i == 1) {
    if ( j== 0) {
        System.out.println("print something ");     
    } else if (j == 1) {
         System.out.println("print something ");     
    }
}

Beware of doing tricky things like combining the numbers to strings and using them as hash keys. It is expensive, and there are semantic traps.

For the UPDATED problem, there is a neat solution:

if (i >= 0 && i <= 1 && j >= 0 && j <= 1) {
    return 1 + i + 2 * j;
}

You can use Map<String,Supplier<Integer>> like this.

Map<String, Supplier<Integer>> map = new HashMap<>();

    map.put("00",()-> logic1());
    map.put("01",()-> 2);
    map.put("10",()-> 3);
    map.put("11",()-> 4);

you can pass every method that it reurns Integer as result.

private int logic1(){
  //your logic... 
  return 1;
}

and use in this way:

map.get(String.valueOf(i)+String.valueOf(j)).get();

Note: in your example, you return 3 as a default, so if i=2 or j=2 for example you would return 3 . Is that the expected behavior? I will provide examples where the values of i and j are assumed to always be 0 or 1

For your specific example:

This seems to be a good compromise, it is shorter and easy enough to read:

if(counterFlag==0) {
    return priorityEnable == 0 ? 0 : 1;
}
return priorityEnable == 0 ? 2 : 3;

For more complex cases (ie 3, 4, or more variables): I'd go for something like this:

Map<int[], Integer> map = new HashMap<>();
map.put(new int[] {0, 0}, 0);
map.put(new int[] {0, 1}, 1);
map.put(new int[] {1, 0}, 2);
map.put(new int[] {1, 1}, 3);

return map.entrySet().stream()
        .filter(e -> e.getKey()[0] == counterFlag)
        .filter(e -> e.getKey()[1] == priorityEnable)
        .map(Map.Entry::getValue)
        .findFirst()
        .orElseThrow(IllegalArgumentException::new);

EDIT: @Holger pointed out that "It's a waste to use a HashMap and then search it linearly. Use IntBuffer as key and you can perform a straight-forward lookup via get"

That is a good point, I tried it out and I think that this is what he meant:

Map<IntBuffer, Integer> map = new HashMap<>();
map.put(IntBuffer.wrap(new int[] {0, 0}), 0);
map.put(IntBuffer.wrap(new int[] {0, 1}), 1);
map.put(IntBuffer.wrap(new int[] {1, 0}), 2);
map.put(IntBuffer.wrap(new int[] {1, 1}), 3);

IntBuffer intBuffer = IntBuffer.wrap(new int[] {counterFlag, priorityEnable});
return map.get(intBuffer);

你也可以这样做...

int value = (i==0 && j==0)?1:(i==0 && j==1)?2:(i==1 && j==0)?3:(i==1 && j==1)?4:null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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