Iterate and compare two list in Java

A quick program to compare two list values inside the lists. This equality check is done using the ArrayList equals[] method and containsAll[] method. Java 8 Stream API Example as well.

1. Introduction


In this article, We'll learn how to compare two ArrayLists for checking Objects 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[]

Read more on how to compare two strings lexicographically.

Example programs are shown on equals[], containsAll[] and java 8 Stream API.

At the end of the article you will be good with comparing two lists and find differences between two lists in java






2. Two List Equality Comparison 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 listOne = new ArrayList[]; listOne.add["super"]; listOne.add["this"]; listOne.add["overlaoding"]; listOne.add["overriding"]; // list 2 List listTwo = new ArrayList[]; 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. List equals[] Method in Java with Examples -Arraylist Equals working

List equals[] method is used to compare the two lists and compare the size of the both lists are same. If not same then returns false. Otherwise, Next it checks the each element is present in the both lists.
package com.javaprogramto.collections.compare;

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

public class ListEqualsExample {

public static void main[String[] args] {

List numbers1List = new LinkedList[];
numbers1List.add[100];
numbers1List.add[300];
numbers1List.add[500];

List numbers2List = new ArrayList[];
numbers2List.add[200];
numbers2List.add[400];
numbers2List.add[600];

boolean bothEquals = numbers1List.equals[numbers2List];

if[bothEquals]{
System.out.println["Both lists are equal"];
} else {
System.out.println["Both lists are not equal"];
}
}
}

Output:
Both lists are not equal

4. 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 listA = new ArrayList[]; listA.add["double"]; listA.add["int"]; listA.add["float"]; listA.add["linkedlist"]; // list 2 List listB = new ArrayList[]; 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 listB = new ArrayList[]; 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

5. Using Java 8 Stream API - Compare Two Lists Of Objects


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.

The below program showshow to compare two ArrayList of different objects inJava 8 using stream api..

public class TwoArrayListEqualityExampleInJava8 { public static void main[String[] args] { // list 1 List list1 = new ArrayList[]; list1.add["compare"]; list1.add["two"]; list1.add["lists"]; list1.add["in java 8"]; // list 2 List list2 = new ArrayList[]; list2.add["compare"]; list2.add["two"]; list2.add["lists"]; list2.add["in java 8"]; List 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

6. 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

How to load logging.properties in java.util.Logging API?

Video liên quan

Chủ Đề