Search code examples
javaif-statementdry

Best DRY if statement?


Let's say I want to compare a bunch of variables to one static variable, normally I would do it like this:

int w = 0;
int x = 1;
int y = 1;
int z = 2;
if(w == x || w == y || w == z){/*more code here*/}

But that can get extremely long and doesn't seem necessary, are there any ways to do something more like:

 if(w == (x || y || z)){/*more code here*/}

I would like to think that there is a way to do it like this.


Solution

  • Instead of:

    if(w == x || w == y || w == z)
    

    you can do:

    if(Arrays.asList(x, y, z).contains(w))