How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?

1. Introduction


In this article, We'll learn how to compare two ArrayLists for checking equality.

ArrayList has an equal() method that takes one argument type of Object. This equals() method compares the passed list object with the current list object. If both lists are having same values then it returns true, otherwise false. equals() and containsAll() method both are part of ArrayList class. ContainsAll() method also works similar way and produces the same result. These two methods may throw runtime exceptions such as ClassCastException, NullPointerException.

Read more on how to compare two strings lexicographically.

Example programs are shown on equals(), containsAll() and java 8 Stream API.

How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?




2. Two List Equality Comparision Example


An example program to check all values in two lists are the same or not. If even anyone's value is different anyone list will return false as a result.

java compare two lists of objects:

package com.java.w3schools.blog.arraylist;

import java.util.ArrayList;
import java.util.List;

/**
*
* Example compare two ArrayList for equality in Java.
*
* @author javaprogramto.com
*
*/
public class TwoArrayListEqualityExample {

public static void main(String[] args) {

// list 1
List<String> listOne = new ArrayList<String>();
listOne.add("super");
listOne.add("this");
listOne.add("overlaoding");
listOne.add("overriding");

// list 2
List<String> listTwo = new ArrayList<String>();
listTwo.add("super");
listTwo.add("this");
listTwo.add("overlaoding");
listTwo.add("overriding");

// comparing two lists
boolean isEqual = listOne.equals(listTwo);
System.out.println(isEqual);

}

}

Output:

true

3. Java compare two lists of objects using containsAll() method


Java ArrayList containsAll() method example to compare two lists are equal or not.

public class TwoArrayListEqualityExample {

public static void main(String[] args) {

// list 1
List<String> listA = new ArrayList<String>();
listA.add("double");
listA.add("int");
listA.add("float");
listA.add("linkedlist");

// list 2
List<String> listB = new ArrayList<String>();
listB.add("super");
listB.add("this");
listB.add("overlaoding");
listB.add("overriding");

// comparing two lists
boolean isEqualAllValues = listA.containsAll(listB);
System.out.println(isEqualAllValues);

}

}

Output:

false


Because the second list listB is having all different values when compared to listA.

Now change the values of listB as in listA.

// list 2
List<String> listB = new ArrayList<String>();
listB.add("double");
listB.add("int");
listB.add("float");
listB.add("linkedlist");

// comparing two lists
boolean isEqualAllValues = listA.containsAll(listB);
System.out.println(isEqualAllValues);


Output:

true

4. Using Java 8 Stream API


Now, it is time to write the same logic using java 8 stream api. Stream API is very powerful but in this case, it is good to use either equals() or containsAll() method which is more comfortable to use for programmers.

public class TwoArrayListEqualityExampleInJava8 {

public static void main(String[] args) {

// list 1
List<String> list1 = new ArrayList<String>();
list1.add("compare");
list1.add("two");
list1.add("lists");
list1.add("in java 8");

// list 2
List<String> list2 = new ArrayList<String>();
list2.add("compare");
list2.add("two");
list2.add("lists");
list2.add("in java 8");

List<String> unavailable = list1.stream().filter(e -> (list2.stream().filter(d -> d.equals(e)).count()) < 1)
.collect(Collectors.toList());

if (unavailable.size() == 0) {
System.out.println("list1 and list2 all values same.");
} else {
System.out.println("list1 and list2 all values are not same.");
}

list1.add("new value added");

unavailable = list1.stream().filter(e -> (list2.stream().filter(d -> d.equals(e)).count()) < 1)
.collect(Collectors.toList());

if (unavailable.size() > 0) {
System.out.println("list1 is modifed. so both lists are having different values");
}

}

}

Output:

list1 and list2 all values same.
list1 is modifed. so both lists are having different values

5. Conclusion


In this article, We've seen


how to compare two lists in java using equals() and containsAll() method. These two methods take List as an argument and compare each and every object are same in each list. equals() method is overridden in ArrayList class.

Find unmatched values from Two lists

GitHub code 1

GitHub code 2

GitHub Code 3

Ref

0 Comments