For a switch case imagine I have 5 different cases and three of these share a common action that I don't want to repeat. Following code illustrates perfectly what I want to achieve (but in a switch):
int x = 5;
if(x == 1) {
System.out.println("one");
} else if (x == 2) {
System.out.println("two");
} else {
String common = "thirty-";
if (x == 3) {
method_one(common);
} else if (x == 4) {
method_two(common);
} else if (x == 5) {
method_three(common);
}
}
Can I write this as a switch case elegantly? My current solution is seen below:
int[] smallGroup = {1,2};
if (!Arrays.asList(smallGroup).contains(x))
common = "thiry-";