Check if 2 list are equal java

The equals() method of List interface compares the specified object with this collection for equality. It returns a Boolean value true if both the lists have same elements and are of the same size.

Syntax

public boolean equals(Object o)

The parameter 'o' represents the object to be compared for equality with this list.

Specified By

equals in interface Collection

Overrides

This method overrides equals in class Object

Returns

The equals method returns a Boolean value 'true' if the specified object is equal to this list else it returns false.

Example 1

import java.util.LinkedList; import java.util.List; public class JavaListEqualsExample1 { public static void main(String[] args) { List list= new LinkedList<>(); for (int i=0;i<11;i++){ list.add(i); } List list1= new LinkedList<>(); for (int i=0;i<11;i++){ list1.add(i); } //It returns a Boolean value true if both lists have the same elements and size.. Boolean bool=list.equals(list1); if (bool) { System.out.println("Both the lists are equal.\n" +"List : "+ list+"\n"+"List1 : "+list1); } else{ System.out.println("Both the lists are equal.\n" +"List : "+ list+"\n"+"List1 : "+list1); } } }

Test it Now

Output:

Both the lists are equal. List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List1 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 2

import java.util.LinkedList; import java.util.List; public class JavaListEqualsExample2 { public static void main(String[] args) { List list= new LinkedList<>(); for (int i=0;i<10;i++){ list.add(i); } List list1= new LinkedList<>(); for (int i=0;i<11;i++){ list1.add(i); } //It returns a Boolean value true if both lists have the same elements and size.. Boolean bool=list.equals(list1); if (bool) { System.out.println("Both the lists are equal.\n" +"List : "+ list+"\n"+"List1 : "+list1); } else{ System.out.println("Both the lists are unequal.\n" +"List : "+ list+"\n"+"List1 : "+list1); } } }

Test it Now

Output:

Both the lists are unequal. List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] List1 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Next TopicJava List

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,

Learn to compare two arraylists in Java with simple examples. We will first test if two arraylists are equal or not. If both lists are not equal, we will find the difference between lists.

The difference in list is equals to another third list which contains either additional elements or missing elements.

Also learn to find common elements between two arraylists.

1. Compare two arraylists for equality

Java program to test if two given lists are equal. To test equality –

  • Sort both lists.
  • Compare both lists using equals() method.

List.equals() method return true if both elements are of same size and both contains same set of elements in exactly same order.

public class ArrayListExample { public static void main(String[] args) { ArrayList listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); Collections.sort(listOne); Collections.sort(listTwo); //Compare unequal lists example boolean isEqual = listOne.equals(listTwo); //false System.out.println(isEqual); //Compare equals lists example ArrayList listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); isEqual = listOne.equals(listThree); //true System.out.println(isEqual); } }

Program output.

false true

2. Compare two arraylists – find additional elements

If two arraylists are not equal and we want to find what are additional elements in first list in comparison to second list then we can use this method.

It uses removeAll() method which removes all elements of second list from first list. It leaves only additonal elements in first list.

public class ArrayListExample { public static void main(String[] args) { ArrayList listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements of second list listOne.removeAll(listTwo); System.out.println(listOne); } }

Program output.

[f]

3. Compare two arraylists – find missing elements

To get the missing elements in first list, which are present in second list, we can reverse the above example. Here we can remove all elements of first list from second list using removeAll() method.

public class ArrayListExample { public static void main(String[] args) { ArrayList listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements from second list listTwo.removeAll(listOne); System.out.println(listTwo); } }

Program output.

[e]

4. Compare two arraylists – find common elements

To find common elements in two arraylists, use List.retainAll() method. This method retains only the elements in this list that are contained in the specified arraylist passed as method argument.

public class ArrayListExample { public static void main(String[] args) { ArrayList listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); listOne.retainAll(listTwo); System.out.println(listOne); } }

Program output.

[a, b, c, d]

Above example will work good in Java 8 as well.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Let us know if you liked the post. That’s the only way we can improve.

This method is used to compare two lists. It compares the lists as, both lists should have the same size, and all corresponding pairs of elements in the two lists are equal.

Syntax:

boolean equals(Object o)

Parameters: This function has a single parameter which is object to be compared for equality.

Returns: This method returns True if lists are equal.

Below programs show the implementation of this method.



Program 1:

import java.util.*;

public class GfG {

    public static void main(String[] args)

    {

        List l = new LinkedList<>();

        l.add(10);

        l.add(15);

        l.add(20);

        System.out.println(l);

        List l2 = new ArrayList();

        l2.add(100);

        l2.add(200);

        l2.add(300);

        System.out.println(l2);

        if (l.equals(l2))

            System.out.println("Equal");

        else

            System.out.println("Not equal");

    }

}

Output: [10, 15, 20] [100, 200, 300] Not equal

Program 2: Below is the code to show implementation of list.addAll() using Linkedlist.

import java.util.*;

public class GfG {

    public static void main(String[] args)

    {

        List l = new LinkedList<>();

        l.add(10);

        l.add(15);

        l.add(20);

        System.out.println(l);

        List l2 = new ArrayList();

        l2.add(10);

        l2.add(15);

        l2.add(20);

        System.out.println(l2);

        if (l.equals(l2))

            System.out.println("Equal");

        else

            System.out.println("Not equal");

    }

}

Output: [10, 15, 20] [10, 15, 20] Equal

Reference:
Oracle Docs


Practice Tags :