Checking Equality of Two List of Strings

Our intention is to check the equality of two list of strings under several conditions.

Checking Equality when order matters

Suppose, we have two list of strings : list1 and list2

List<String> list1 = new ArrayList<>();

List<String> list2 = new ArrayList<>();

We are going to push two strings in our each list.

list1.add("A");

list1.add("B");

list2.add("B");

list2.add("A");

Now the present condition of our list is:

list1 = ["A","B"]  and list2 = ["B","A"]

As we want, list1 shouldn’t be equal to list2, so following statement should return false. Because, although the elements are same but their order is different.

list1.equals(list2); //will return false

Now, lets consider another list of strings.

List<String> list3 = new ArrayList<>();

list3.add("A");

list3.add("B");

Here, list1 and list3 will be equals as both of them have same elements and same order.

list1.equals(list3);  //will return true

Checking equality independent of order

If we want to check independent of order, we could copy all of the elements to Set and use equals function on the resulting Set.

Set<Object> set1 = new HashSet<Object>();
set1.addAll(list1);

Set<Object> set2 = new HashSet<Object>();
set2.addAll(list2);

set1.equals(set2) //will return true

But there is a problem with this approach. It will return true for this type of combination also: ["A", "B", "A"], ["A", "B", "B"]

List<String> list4 = new ArrayList<>();

list4.add("A");

list4.add("B");

list4.add("A");   //list4 = ["A", "B", "A"]

List<String> list5 = new ArrayList<>();

list5.add("A");

list5.add("B");

list5.add("B");   //list5 = ["A", "B", "B"]

Set<Object> set4 = new HashSet<Object>();
set4.addAll(list4);

Set<Object> set5 = new HashSet<Object>();
set5.addAll(list5);

set4.equals(set5) //will return true

Nope, I want [“A”,”B”] and [“B”,”A”] to be same, ignore other cases…

Then the best approach is sort the list first and then check equality with equals function. It will return true independent of order and maintaining list size same.

List<String> list1 = new ArrayList<>();

List<String> list2 = new ArrayList<>();

List<String> list3 = new ArrayList<>();

List<String> list4 = new ArrayList<>();

 

list1.add("A");

list1.add("B");

 

list2.add("B");

list2.add("A");

 

list3.add("A");

list3.add("B");

 

list4.add("A");

list4.add("B");

list4.add("A");

 

Collections.sort(list1);

Collections.sort(list2);

Collections.sort(list3);

Collections.sort(list4);

 

list1.equals(list2);  //will return true

list1.equals(list3);  //will return true

list1.equals(list4);  //will return false

That’s it. Happy coding…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s