Let's say you have a string array arr with 3 strings in it. To compare its values, you would simply do the following:
if (arr[0].equals(arr[1]) && arr[0].equals(arr[2] && arr[1].equals(arr[2]) {
return true;
}
But what if that array had hundreds of strings? What is the best way to compare all the values?
I thought of using for loops but Java does not allow loops inside a conditional. Any ideas?
-
9But it does allow conditionals inside a loop...azurefrog– azurefrog2016年02月15日 01:19:01 +00:00Commented Feb 15, 2016 at 1:19
-
4Also, your third check is not needed. IF a == b AND a == c, then we already know that b == c, due to transitivity. It's enough to check whether each element is equal to the first.azurefrog– azurefrog2016年02月15日 01:23:05 +00:00Commented Feb 15, 2016 at 1:23
-
1A better approach would be to just not put duplicate strings in your data structure. Which means you should look to use a hashtable or something similar. Ends up with O(1) efficiency.Tdorno– Tdorno2016年02月15日 01:28:33 +00:00Commented Feb 15, 2016 at 1:28
-
What version of Java are you using?Jason– Jason2016年02月15日 01:39:23 +00:00Commented Feb 15, 2016 at 1:39
-
@azurefrog I am trying to put a loop inside a conditional, not a conditional inside a loop. For instance, if(for loop){}Dragneel– Dragneel2016年02月15日 01:51:44 +00:00Commented Feb 15, 2016 at 1:51
4 Answers 4
How about this 1-liner:
return Arrays.stream(arr).distinct().count() == 1;
This code neatly handles empty (but not null) arrays, returning false
if empty.
If you want to return true
when the array is empty, change the test to:
return Arrays.stream(arr).distinct().count() < 2;
-
What about in the case of a 2-dimensional array, and let's say you intend on only comparing the values of the second row for an example.Dragneel– Dragneel2016年02月15日 01:42:19 +00:00Commented Feb 15, 2016 at 1:42
-
would 'return Arrays.stream(arr[1]).distinct().count() == 1;' work? or no?Blue Boy– Blue Boy2016年02月15日 01:43:02 +00:00Commented Feb 15, 2016 at 1:43
-
Nope. I just tested this with a 1D array and works fine. But it doesn't work with a 2D array.Dragneel– Dragneel2016年02月15日 01:49:20 +00:00Commented Feb 15, 2016 at 1:49
-
Did you change the 'arr' to 'arr[1]'?Blue Boy– Blue Boy2016年02月15日 01:52:41 +00:00Commented Feb 15, 2016 at 1:52
-
I have. The result was return Arrays.stream(arr[1]).distinct().count() == 1;Dragneel– Dragneel2016年02月15日 01:53:54 +00:00Commented Feb 15, 2016 at 1:53
A brute force method to do this is to compare the 1st string with every other string in the array.
public boolean allUnique(String[] arr){
//Assuming the array has at least 1 element.
String s = arr[0];
for(int i = 1; i < arr.length; i++){
if(!arr[i].equals(s)){
return false;
}
}
return true;
}
-
This approach was one of my attempts, but I actually need the method to return true rather than false after checking all values.Dragneel– Dragneel2016年02月15日 01:35:53 +00:00Commented Feb 15, 2016 at 1:35
-
@LawrenceLelo you can return true after the loop. If all comparisons match then it will return true.Atri– Atri2016年02月15日 01:37:45 +00:00Commented Feb 15, 2016 at 1:37
If the array could be of any dimension, then the Objects.deepEquals()
method might be of help:
boolean allEqual = Arrays.stream(arr).allMatch(a -> Objects.deepEquals(arr[0], a));
Even better:
boolean allEqual = Arrays.stream(arr, 1, arr.length) // bounds check left
.allMatch(a -> Objects.deepEquals(arr[0], a)); // to the reader :)
Test:
String[][] arr = {
{"a", "a"},
{"a", "a"},
{"a", "a"}};
boolean allEqual = Arrays.stream(arr, 1, arr.length)
.allMatch(a -> Objects.deepEquals(arr[0], a));
System.out.println(allEqual); // true
-
I just tested this code with a 2D array. It compiles but it is returning false.Dragneel– Dragneel2016年02月15日 02:01:22 +00:00Commented Feb 15, 2016 at 2:01
-
@LawrenceLelo What's your check? It's returning
true
to me for a 2d array.fps– fps2016年02月15日 02:08:16 +00:00Commented Feb 15, 2016 at 2:08 -
I was testing the code with an array of objects rather strings. Though, the objects do return a string with the toString() method. Perhaps my results are different because I'm using an array of objects instead of strings.Dragneel– Dragneel2016年02月15日 02:23:04 +00:00Commented Feb 15, 2016 at 2:23
-
@LawrenceLelo If you are using objects, make sure they implement equals() consistently.fps– fps2016年02月15日 03:29:40 +00:00Commented Feb 15, 2016 at 3:29
for(int i = 0; i < arr.length-1; i++){
if(!arr[i].equals(arr[i+1])){
return false;
}
}
return true;