Although there is an accepted answer, I also want to share my ways:
Method 1 is similar to the accepted answer. However, instead of using List, I use Set. In this case, it can be even faster than doing == individually if there are many values ββto check:
// make it a static final member if semantically possible Set<Integer> ALL_VALUES = new HashSet<Integer>(Arrays.asList(a,b,c,d,e,f,g,h)); //..... if (ALL_VALUES.contains(w)) { //... do something }
Method 2 is to write a small useful function, something like
public static <T> boolean sameAsAny(T value, T... possibleValues) { for (T p : possibleValues) { if (value == p) { return true; } } return false; }
with this use you can do something like:
if (sameAsAny(w, x, y, z))
source share