Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays Equals Ignoring Order [duplicate]

Possible Duplicate:
Java: Checking equality of arrays (order doesnt matter)

I have two arrays :

String[] a1 = {"a", "b", "c"}; String[] a2 = {"c", "b", "a"}; 

I need to check if both contains same elements (and of same length) irrespective of order of elements.

I tried Arrays.equals(a1, a2) but it considers order of element. org.apache.commons.lang.ArrayUtils does not provide this thing.

I know I can achieve the same by creating my own method (checking for same length, then sorting both array and then using Arrays.equals(a1, a2)) but wanted to know if this thing is provided in any API or there is more smart way to do the same.

like image 555
Anil Bharadia Avatar asked Aug 17 '12 16:08

Anil Bharadia


People also ask

Does order matter in array Java?

Java: Checking equality of arrays (order doesn't matter)

Can you use .equals for arrays?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How do you compare two lists irrespective orders in Java?

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

How do you compare two arrays equal in Java?

Arrays. equals(Object[] a, Object[] a2) method returns true if the two specified arrays of objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

How to check if two arrays are equal or not?

Check if two arrays are equal or not. Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though. Note : If there are repetitions, then counts...

How to compare two arrays before comparing them?

If it's just integers or other primitive values, you can sort () them before comparing. If its objects, combine it with the map () function to extract an identifier that will be compared Show activity on this post. Show activity on this post. Which expects that an array contains exactly the elements listed, in any order.

Is the number of elements in two arrays the same?

Here the number of elements in both arrays is the same and both arrays contain all elements from the other array, yet they are different arrays and the test should fail.

How to do order agnostic comparison in Java?

There are different ways to do order agnostic comparison. Let's take a look at them one by one. 3. Using JUnit JUnit is a well-know framework used for unit testing in the Java ecosystem. We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods.


1 Answers

If you have these arrays in something inheriting from Collection, you can just use collection.containsAll( otherCollection ) from the Collection interface. However, you'll also need to compare the lengths of the two to verify that one set isn't a superset of the other.

(Thanks go to Aardvarkk and piegames.)

http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#containsAll(java.util.Collection)

Note: This will work up to a point. This is defined to check for at least one of any element existing. That is, if you have 3 a values in one collection, and 7 a values in the other, that will not necessarily cause it to call them unequal.

Examples:

[a, b, c] == [c, a, b]             // Works -- Mixed order [a, b, c, d, d] == [a, b, d, c, d] // Works -- Mixed order with repeats [a, b, c, d, d] == [a, b, b, c, d] // FAILS -- Different repeats [a, b, c, d, d] != [a, b, c, d]    // Works -- Length differs with repeats [a, b, c, d] != [a, b, c]          // Works -- Length differs [a, b, d] != [a, b, c]             // Works -- Disjoint sets 
like image 94
BlackVegetable Avatar answered Oct 05 '22 23:10

BlackVegetable