Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there clean syntax for checking if multiple variables all have the same value?

Tags:

java

We have n variables X = {x1,x2,...xn} they are not in any structures whatsoever.

In python for example I can do that: if (x1 == x2 == x3 == xn):

In java I must do: if((x1 == x2) && (x2 == x3) && (x3 == xn)):

Do you know a simple way to improve this syntax? (Imagine very long variable name and lot of them)

Thanks.

like image 352
Pierre Guilbert Avatar asked Mar 31 '11 16:03

Pierre Guilbert


People also ask

How do you check if a variable is equal to multiple values?

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable. Copied! We used the logical AND (&&) operator to chain multiple equality checks.

How do you know if three variables are equal?

To have a comparison of three (or more) variables done correctly, one should use the following expression: if (a == b && b == c) .... In this case, a == b will return true, b == c will return true and the result of the logical operation AND will also be true.

How do I test multiple variables in Python?

To test multiple variables x , y , z against a value in Python, use the expression value in {x, y, z} . Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value.


2 Answers

If you have lots of these variables, have you considered putting them in a collection instead of having them as separate variables? There are various options at that point.

If you find yourself doing this a lot, you might want to write helper methods, possibly using varargs syntax. For example:

public static boolean areAllEqual(int... values) {     if (values.length == 0)     {         return true; // Alternative below     }     int checkValue = values[0];     for (int i = 1; i < values.length; i++)     {         if (values[i] != checkValue)         {             return false;         }     }     return true; } 

An alternative as presented by glowcoder is to force there to be at least one value:

public static boolean areAllEqual(int checkValue, int... otherValues) {     for (int value : otherValues)     {         if (value != checkValue)         {             return false;         }     }     return true; } 

In either case, use with:

if (HelperClass.areAllEqual(x1, x2, x3, x4, x5)) {     ... } 
like image 147
Jon Skeet Avatar answered Sep 21 '22 16:09

Jon Skeet


You could create a utility method like this:

public boolean allEqual(Object... objs) {     if(objs.length < 2) return true; // 0 or 1 objects are all equal     Object key = objs[0]; // pick one     for(Object o : objs) if(!o.equals(key)) return false;     return true; } 

Another option would be

public boolean allEqual(Object key, Object... objs) {     for(Object o : objs) if(!o.equals(key)) return false;     return true; } 

To simplify a lot of boilerplate logic. Then just go

if(allEqual(x,x1,x2,x3)) 

Obviously the two are mutually exclusive (they are signaturely ambigous) but you could have allEqual and allEqualWithKey

like image 39
corsiKa Avatar answered Sep 19 '22 16:09

corsiKa