Is there a simpler way to check multiple values against one value in an if-statement?

前端 未结 12 776
名媛妹妹
名媛妹妹 2020-11-27 07:55

Basically, what I want to do is check two integers against a given value, therefore, classically what you would do is something like this:

//just to get some         


        
相关标签:
12条回答
  • 2020-11-27 08:13

    In Java 8 we can achieve the same by using the below method:

    private boolean methodName(int variant,int... args){
            if(args.length > 0){ return Arrays.stream(args).anyMatch( x -> x == variant); }
            return false;
        }
    

    The given method will return true if the variant will match any possible input(s). This is used for or condition.

    In the same way, if you want to do &&(and) condition then you just need to used other Java 8 methods:

    Note: These methods take Predicate as an argument.

    anyMatch: return true the moment the first predicate returns true otherwise false.

    allMatch: return true if all the predicates return true otherwise false.

    noneMatch: return true if none of the predicates return true otherwise false.

    Performance Note: This is good when you have enough amount of data to check as it has some overhead but it works really well when you use this for enough amount of data. normal way is good for just two conditions.

    0 讨论(0)
  • 2020-11-27 08:15

    I think that a bit-wise OR:

    if ((a | b) == 0) . . .
    

    would work if you want to check specifically for 0. I'm not sure if this saves any execution time. More to the point, it makes for cryptic code, which will make the future maintainer of this code curse you (even if its yourself). I recommend sticking with the more-typing option.

    Bah. I misread OP's original logic.

    Another go...

    If you want to test whether any one of many variables is equal to an expected value, a generic function might work:

    public <T> boolean exists(T target, T... values) {
        for (T value : values) {
            if (target == null) {
                if (value == null) {
                    return true;
                }
            } else if (target.equals(value)) {
                return true;
            }
        }
        return false;
    }
    

    This will work for any number of objects of one type. Primitives will be autoboxed so it will work with them as well. Your original code will be something like:

    if (test(0, a, b)) {
        // do something
    }
    

    (A better method name would be desperately needed to even consider whether this an improvement over what you have now. Even if the test expands to 3 or 4 variables, I question the need for this kind of thing.) Note that this also works with arrays:

    int[] values = { . . . };
    if (test(0, values)) { . . .
    

    and it can be used to test whether an array (or any of a collection of variables) is null.

    0 讨论(0)
  • 2020-11-27 08:16

    Unfortunately there is no such construct in Java.

    It this kind of comparison is frequent in your code, you can implement a small function that will perform the check for you:

    public boolean oneOfEquals(int a, int b, int expected) {
        return (a == expected) || (b == expected);
    }
    

    Then you could use it like this:

    if(oneOfEquals(a, b, 0)) {
        // ...
    }
    

    If you don't want to restrict yourselft to integers, you can make the above function generic:

    public <T> boolean oneOfEquals(T a, T b, T expected) {
        return a.equals(expected) || b.equals(expected);
    }
    

    Note that in this case Java runtime will perform automatic boxing and unboxing for primitive types (like int), which is a performance loss.

    0 讨论(0)
  • 2020-11-27 08:18
    if(a == 0 || b == 0)
    {
    //Then do something
    }
    

    Why not keep it readable? What is not concise about this? On the other hand,

    a = (int)(Math.random()*5);
    

    involves an unnecessary cast. Why not just use Random and invoke nextInt()?

    0 讨论(0)
  • 2020-11-27 08:22

    For this example, you can do

    if (a * b == 0)
    

    or for more variables

    if (a * b * c * d == 0)
    

    while more concise it may not be as clear. For larger values, you need to cast to a long to avoid an overflow.

    0 讨论(0)
  • 2020-11-27 08:22

    Here's a modification of @buc's answer that can take any number of any arguments:

    public <T> boolean oneOfEquals(T expected, T... os) {
        for (T o : os) {
            if (expected.equals(o)) return true;
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题