Reputation: 83
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;
}
Upvotes: 6
Views: 7656
Reputation: 719669
The best I can think of1 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;
}
... but I'm not sure I would write that in my code. It is obscure, and that kind of trickiness can prevent the JIT compiler from generating optimal code2.
1 - I do NOT claim this is "best practice". This is just my opinion.
2 - That might be irrelevant ...
Upvotes: 4
Reputation: 9766
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 (i.e. 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);
Upvotes: 1
Reputation: 94
you can do this too...
int value = (i==0 && j==0)?1:(i==0 && j==1)?2:(i==1 && j==0)?3:(i==1 && j==1)?4:null;
Upvotes: 0
Reputation: 17299
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();
Upvotes: 2