I'm looking for a method to detect if all objects within an array(list) are the same. e. g:

arraylist1 = {"1", "1", "1", "1"} // elements are the same arraylist2 = {"1", "1", "0", "1"} // elements are not the same 

Thanks for help

7

9 Answers

Java 8 solution :

boolean match = Arrays.stream(arr).allMatch(s -> s.equals(arr[0])); 

Same logic for lists :

boolean match = list.stream().allMatch(s -> s.equals(list.get(0))); 

It comes quite complicated if there are only or any null values in the array (resulting in a NullPointerException). So possible workarounds are:

  • Using Predicate.isEqual, it uses the static method equals from the Objects class and it will do the null check for you before calling equals on the first argument.

    boolean match = Arrays.stream(arr).allMatch(Predicate.isEqual(arr[0]));
    boolean match = Arrays.stream(arr).allMatch(s -> Objects.equals(arr[0], s));

  • Using distinct() and count() :

    boolean match = Arrays.stream(arr).distinct().count() == 1;

    that can be improved into Arrays.stream(arr).distinct().limit(2).count() == 1; as there is no need to check the all pipeline's content if you already find 2 distinct elements.

8
public static boolean AreAllSame(String[] array) { boolean isFirstElementNull = array[0] == null; for(int i = 1; i < array.length; i++) { if(isFirstElementNull) if(array[i] != null) return false; else if(!array[0].equals(array[i])) return false; } return true; } 

Please feel free to correct any syntax mistakes. I fear my Java-fu may be lacking today.

11
if( new HashSet<String>(Arrays.asList(yourArray)).size() == 1 ){ // All the elements are the same } 
13

If your list is empty return true. If not, loop through it and check if all elements are equal to the element at index 0. If so, return true, otherwise return false.

 public boolean containsSameValues(int[] array) { if(array.length == 0) { throw new IllegalArgumentException("Array is empty"); } int first = array[0]; for(int i=0;i<array.length;i++) { if(array[i] != first) { return false; } } return true; } 
2
boolean containsAllValues=false; boolean t=false; int isto=0; for(int k=0;k<arraylist1.size();k++){ for(int n=0;n<arraylist2.size();n++){ if(arraylist1.get(k).equals(arraylist2.get(n))){ t=true; } } if(t){ isto++; }else{ isto=0; break; } } if(isto!=0){ containsAllValues=true; } 

With this you can check if arraylist2 contains values from arraylist1.

You can sort the array and then use the Array equals method:

public boolean equalArrays(int []f ,int [] g){ Arrays.sort(f); Arrays.sort(g); if (Arrays.equals(f, g)) return true; return false; } 

To test it out:

 int [] h={1,1,0,1}; int [] t={1,1,1,0}; System.out.println(cc.equalArrays(h,t)); 
0

For Java 8, Alexis C's solution is probably the best. However, if you're stuck with <= Java 7 and don't think David Wallace's approach is expressive enough, you could also try this:

boolean result = Collections.frequency(yourList, "1") == yourList.size(); 

Basically what this does is that it checks whether the number of elements in your list equal to "1" matches the total number of elements in the list. Pretty straightforward.

By the way -- this is pure Java Collections API, and I believe Collection.frequency(..) has been there since at least JDK 1.5. See the javadocs for more information.

EDIT: Here's a quick fiddle if you want to take this for a test drive.

1

The easiest one:

public static boolean checkTheSame(int[] numbers) { for (int i = 0; i < numbers.length - 1; i++) { if (numbers[i] != numbers[i + 1]) { return false; } } return true; } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy