4

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?

asked Feb 15, 2016 at 1:18
6
  • 9
    But it does allow conditionals inside a loop... Commented Feb 15, 2016 at 1:19
  • 4
    Also, 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. Commented Feb 15, 2016 at 1:23
  • 1
    A 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. Commented Feb 15, 2016 at 1:28
  • What version of Java are you using? Commented 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){} Commented Feb 15, 2016 at 1:51

4 Answers 4

5

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;
answered Feb 15, 2016 at 1:32
5
  • 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. Commented Feb 15, 2016 at 1:42
  • would 'return Arrays.stream(arr[1]).distinct().count() == 1;' work? or no? Commented 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. Commented Feb 15, 2016 at 1:49
  • Did you change the 'arr' to 'arr[1]'? Commented Feb 15, 2016 at 1:52
  • I have. The result was return Arrays.stream(arr[1]).distinct().count() == 1; Commented Feb 15, 2016 at 1:53
1

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;
}
answered Feb 15, 2016 at 1:29
2
  • This approach was one of my attempts, but I actually need the method to return true rather than false after checking all values. Commented Feb 15, 2016 at 1:35
  • @LawrenceLelo you can return true after the loop. If all comparisons match then it will return true. Commented Feb 15, 2016 at 1:37
1

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
answered Feb 15, 2016 at 1:55
4
  • I just tested this code with a 2D array. It compiles but it is returning false. Commented Feb 15, 2016 at 2:01
  • @LawrenceLelo What's your check? It's returning true to me for a 2d array. Commented 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. Commented Feb 15, 2016 at 2:23
  • @LawrenceLelo If you are using objects, make sure they implement equals() consistently. Commented Feb 15, 2016 at 3:29
0
for(int i = 0; i < arr.length-1; i++){
 if(!arr[i].equals(arr[i+1])){
 return false;
 }
}
return true;
answered Feb 15, 2016 at 1:23
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.