Java Program to Compare Two ArrayList in Java

In the previous article, we have seen Java Program to Check if ArrayList is Empty

In this article we are going to see various ways to compare two ArrayList in java.

Java Program to Compare Two ArrayList in Java

Let’s see different ways to compare two ArrayList.

Method-1:  Java Program to Compare Two ArrayList in Java By Using equals( ) function

The equals() function takes two arraylists as input and compares them directly.

Approach:

  • Create an arraylist and add some elements to it
  • Display the elements
  • Create another arraylist with same elements
  • Display the elements
  • Pass both the arraylists into the user-defined function checkSameArrayLists( ) which will print the result. It uses the equals( ) method to check if both the arraylists are equal or not.
  • Repeat the above steps for another arraylist with different elements.
  • Pass the first and third arraylist into the user defined function.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Displaying the list
        System.out.println("Arraylist 1 "+arr);
        // Creating an empty ArrayList
        ArrayList<String> arr2 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr2.add("One");
        arr2.add("Two");
        arr2.add("Three");
        System.out.println("Arraylist 2 "+arr2);
        // Displaying the list
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 2...");
        checkSameArrayLists(arr,arr2);
        ArrayList<String> arr3 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr3.add("A");
        arr3.add("B");
        arr3.add("C");
        // Displaying the list
        System.out.println("Arraylist 3 "+arr3);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 3...");
        checkSameArrayLists(arr,arr3);
        
    }
    //Function that checks and prints if two ArrayLists are same
    static void checkSameArrayLists(ArrayList a,ArrayList b)
    {
        //Returns boolean values for the condition
        if(a.equals(b))
            System.out.println("The arraylists are same");
        else
            System.out.println("The arraylists are not same");
    }
}
Output:

Arraylist 1 [One, Two, Three]
Arraylist 2 [One, Two, Three]
Comparing arraylist 1 and 2...
The arraylists are same
Arraylist 3 [A, B, C]
Comparing arraylist 1 and 3...
The arraylists are not same

Method-2: Java Program to Compare Two ArrayList in Java By Using removeAll( ) function

removeAll( )  function takes collection as input and then removes all elements from the arraylist containing in the other arraylist. The leftover elements are the uncommon elements.

Approach:

  • Create an arraylist and add some elements to it.
  • Display the elements.
  • Create another arraylist with same elements.
  • Display the elements.
  • Pass both the arraylists into the user-defined function checkSameArrayLists( ) which will print the uncommon elements. It uses the removeAll( ) method to remove all the common elements from the arraylist.
  • Display the modified arraylist.
  • Repeat the above steps for another arraylist with different elements.
  • Pass the first and third arraylist into the user defined function.
  • Display the modified arraylist.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Displaying the list
        System.out.println("Arraylist 1 "+arr);
        // Creating an empty ArrayList
        ArrayList<String> arr2 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr2.add("One");
        arr2.add("Two");
        arr2.add("Three");        
        // Displaying the list
        System.out.println("Arraylist 2 "+arr2);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 2...");
        checkSameArrayLists(arr,arr2);
        ArrayList<String> arr3 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr3.add("A");
        arr3.add("B");
        arr3.add("C");
        // Displaying the list
        System.out.println("Arraylist 3 "+arr3);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 3...");
        checkSameArrayLists(arr,arr3);
        
    }
    //Function that prints the uncommon elements
    static void checkSameArrayLists(ArrayList a,ArrayList b)
    {
        //Removes the common elements
        b.removeAll(a);
        System.out.println("The un-common elements are"+b);
    }
}

Output:

Arraylist 1 [One, Two, Three]
Arraylist 2 [One, Two, Three]
Comparing arraylist 1 and 2...
The un-common elements are[]
Arraylist 3 [A, B, C]
Comparing arraylist 1 and 3...
The un-common elements are[A, B, C]

Method-3: Java Program to Compare Two ArrayList in Java By Using retainAll( ) function

retainAll( )  function takes arraylist as input and then removes all elements from the arraylist that are not there in the other arraylist. The leftover elements are the common elements.

  • Create an arraylist and add some elements to it.
  • Display the elements.
  • Create another arraylist with same elements.
  • Display the elements.
  • Pass both the arraylists into the user-defined function checkSameArrayLists( ) which will print the uncommon elements. It uses the retainAll( ) method to remove all the uncommon elements from the arraylist.
  • Display the modified arraylist.
  • Repeat the above steps for another arraylist with different elements.
  • Pass the first and third arraylist into the user defined function.
  • Display the modified arraylist.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Displaying the list
        System.out.println("Arraylist 1 "+arr);
        // Creating an empty ArrayList
        ArrayList<String> arr2 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr2.add("One");
        arr2.add("Two");
        arr2.add("Three");        
        // Displaying the list
        System.out.println("Arraylist 2 "+arr2);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 2...");
        checkSameArrayLists(arr,arr2);
        ArrayList<String> arr3 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr3.add("A");
        arr3.add("B");
        arr3.add("C");
        // Displaying the list
        System.out.println("Arraylist 3 "+arr3);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 3...");
        checkSameArrayLists(arr,arr3);
        
    }
    //Function that prints the common elements
    static void checkSameArrayLists(ArrayList a,ArrayList b)
    {
        //Retains the common elements
        b.retainAll(a);
        System.out.println("The common elements are"+b);
    }
}

Output:

Arraylist 1 [One, Two, Three]
Arraylist 2 [One, Two, Three]
Comparing arraylist 1 and 2...
The common elements are[One, Two, Three]
Arraylist 3 [A, B, C]
Comparing arraylist 1 and 3...
The common elements are[]

Method-4: Java Program to Compare Two ArrayList in Java By Using contentEquals( ) function

contentEquals( )  function takes arraylist as stringbuffer and then checks if the contents of both the arraylists are same or not.  It returns Boolean values.

  • Create an arraylist and add some elements to it.
  • Display the elements.
  • Create another arraylist with same elements.
  • Display the elements.
  • Pass both the arraylists as strings into the user-defined function checkSameArrayLists( ) which uses contentEquals( ) method to check if both the arraylists are equal.
  • Display the result.
  • Repeat the above steps for another arraylist with different elements.
  • Pass the first and third arraylist into the user defined function.
  • Display the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Displaying the list
        System.out.println("Arraylist 1 "+arr);
        // Creating an empty ArrayList
        ArrayList<String> arr2 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr2.add("One");
        arr2.add("Two");
        arr2.add("Three");        
        // Displaying the list
        System.out.println("Arraylist 2 "+arr2);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 2...");
        checkSameArrayLists(arr,arr2);
        ArrayList<String> arr3 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr3.add("A");
        arr3.add("B");
        arr3.add("C");
        // Displaying the list
        System.out.println("Arraylist 3 "+arr3);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 3...");
        checkSameArrayLists(arr,arr3);
        
    }
    //Function that compares the arraylists
    static void checkSameArrayLists(ArrayList a,ArrayList b)
    {
        // Converts the arraylist into strings then compares them
        if(a.toString().contentEquals(b.toString()))
            System.out.println("The arrayLists are equal");
        else
            System.out.println("The arrayLists are not equal");
    }
}

Output:

Arraylist 1 [One, Two, Three]
Arraylist 2 [One, Two, Three]
Comparing arraylist 1 and 2...
The arrayLists are equal
Arraylist 3 [A, B, C]
Comparing arraylist 1 and 3...
The arrayLists are not equal

Method-5: Java Program to Compare Two ArrayList in Java By Using JAVA stream API

Java stream has a filter( ) function that filters the common elements from both the arraylists and then we will use Collections.toList( ) function to print the common  elements as an arrayList.

Approach:

  • Create an arraylist and add some elements to it.
  • Display the elements.
  • Create another arraylist with same elements.
  • Display the elements.
  • Pass both the arraylists into the stream( ).filter() function that filters the common elements and then use Collections.toList( ) to convert it into a list and display.
  • Repeat the above steps for another arraylist with different elements.
  • Pass the first and third arraylist into the user defined function.

Program:

import java.util.*;
import java.util.stream.Collectors;
public class Main
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Displaying the list
        System.out.println("Arraylist 1 "+arr);
        // Creating an empty ArrayList
        ArrayList<String> arr2 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr2.add("One");
        arr2.add("Two");
        arr2.add("Three");        
        // Displaying the list
        System.out.println("Arraylist 2 "+arr2);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 2...");
        checkSameArrayLists(arr,arr2);
        ArrayList<String> arr3 = new ArrayList<String>();
        // Adding elements to the arrayList
        arr3.add("A");
        arr3.add("B");
        arr3.add("C");
        // Displaying the list
        System.out.println("Arraylist 3 "+arr3);
        // Comparing arraylist with the first arraylist
        System.out.println("Comparing arraylist 1 and 3...");
        checkSameArrayLists(arr,arr3);
        
    }
    //Function that prints the common elements
    static void checkSameArrayLists(ArrayList a,ArrayList b)
    {
    // Compares both the arraylists and then prints the common elements
    System.out.println("The common elements are"+a.stream().filter(b::contains).
    collect(Collectors.toList()));

    }
}


Output:

Arraylist 1 [One, Two, Three]
Arraylist 2 [One, Two, Three]
Comparing arraylist 1 and 2...
The common elements are[One, Two, Three]
Arraylist 3 [A, B, C]
Comparing arraylist 1 and 3...
The common elements are[]

Are you new to the java programming language? We recommend you to ace up your practice session
with these Basic Java Programs Examples

Related Java Programs: