Reputation: 99
I am writing a program for a class assignment and I want to compare multiple items in an array for equivalence. My statement basically looks like:
if (a == b && b == c && c == d && d == e && e == f) {
// do stuff
}
This condition seems incredibly verbose, and I am wondering if there's a shorter way to write this.
Upvotes: 4
Views: 3700
Reputation: 58281
My suggestion is to create a set and if its size is one then all elements are equal, otherwise not:
public static < E > boolean areAllEqual( E[] inputArray ){
Set<E> mySet = new HashSet<E>(Arrays.asList(inputArray));
return mySet.size() == 1;
}
Following is complete code example:
import java.util.*;
public class HelloWorld{
public static < E > boolean areAllEqual( E[] inputArray ){
Set<E> mySet = new HashSet<E>(Arrays.asList(inputArray));
return mySet.size() == 1;
}
public static void main(String []args){
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'H', 'H', 'H', 'H' };
System.out.println("It should work");
if (areAllEqual(intArray))
System.out.println("inarray Yes");
else
System.out.println("inarray No");
if (areAllEqual(doubleArray))
System.out.println("doubleArray Yes");
else
System.out.println("doubleArray No");
if(areAllEqual(charArray))
System.out.println("charArray Yes");
else
System.out.println("charArray No");
}
}
output:
It should work
inarray No
doubleArray No
charArray Yes // all elements in char array are eq
Upvotes: 3
Reputation: 69349
This could be done in Java 8 the following, given that you already have an array:
int[] numbers = {1, 2, 3, 4, 5};
if (Arrays.stream(numbers).allMatch(i -> (i == numbers[0]))) {
//they are equal
}
What it does is:
IntStream
(it does not create extra objects, apart from the IntStream
)IntPredicate
.IntPredicate
is i -> (i == numbers[0])
, which maps i
to (i == numbers[0])
.It would be advised to encapsulate this in a helper method, and you'd want to early bail if the array is empty, like this:
public static boolean allEquals(final int[] input) {
return (input.length > 0 && Arrays.stream(input).allMatch(i -> i == input[0]));
}
This depends though on whether it should return true
or false
if there's no input... Is everything equal then?
Please note that you'd need to make this code for all primitives you are using. As an extra, you can also provide a generic version:
public static <T> boolean allEquals(final T[] input) {
return (input.length > 0 && Arrays.stream(input).allMatch(i -> i.equals(input[0])));
}
Now when you do not have a suitable structure, meaning they are most likely just variables, then you will need to decide whether you are needing the extra performance or can live with the overhead of passing them in as an varargs input, that implicitely creates an array, the code for that would be really similar:
public static <T> boolean allEquals(final T... input) {
return (input.length > 0 && Arrays.stream(input).allMatch(i -> i.equals(input[0])));
}
You can almost certainly live with the extra overhead, only in very performance critical applications, you need to wary if your method gets executed a load of times.
Upvotes: 1
Reputation: 77474
This may appear to be verbose, but at least it is reasonably efficient and clear.
Don't overdo it with optimizing code for brevity.
You could easily make a helper function
boolean allEqual(Object ... objs) {
for(int i = 1; i < obj.length; i++) {
if (!objs[i].equals(objs[0])) {
return false;
}
}
return true;
}
But this will create additional objects; in particular with primitive values:
if (allEqual(1.1,2.1,3.1,4.1))
will create 5 objects, that need to be garbage collected.
Make sure you actually want ==
and not equals
.
Upvotes: 3