Java difference between two lists

Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

Example:

Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [1, 2, 3, 4] Output: Array List are equal Input : ArrayList1 = [1, 2, 3, 4], ArrayList2 = [4, 2, 3, 1] Output: Array List are not equal

Syntax:

boolean equals(Object o)

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

Returns: This method returns True if Array lists are equal.

Implementation:

import java.util.ArrayList;

public class GFG {

    public static void main(String[] args)

    {

        ArrayList<String> ArrayList1

            = new ArrayList<String>();

        ArrayList<String> ArrayList2

            = new ArrayList<String>();

        ArrayList1.add("item 1");

        ArrayList1.add("item 2");

        ArrayList1.add("item 3");

        ArrayList1.add("item 4");

        ArrayList2.add("item 1");

        ArrayList2.add("item 2");

        ArrayList2.add("item 3");

        ArrayList2.add("item 4");

        System.out.println(" ArrayList1 = " + ArrayList2);

        System.out.println(" ArrayList1 = " + ArrayList1);

        if (ArrayList1.equals(ArrayList2) == true) {

            System.out.println(" Array List are equal");

        }

        else

        {

            System.out.println(" Array List are not equal");

        }

        System.out.println(

            "\n Lets insert one more item in Array List 1");

        ArrayList1.add("item 5");

        System.out.println(" ArrayList1 = " + ArrayList1);

        System.out.println(" ArrayList = " + ArrayList2);

        if (ArrayList1.equals(ArrayList2) == true) {

            System.out.println(" Array List are equal");

        }

        else {

            System.out.println(" Array List are not equal");

        }

    }

}

Output ArrayList1 = [item 1, item 2, item 3, item 4] ArrayList1 = [item 1, item 2, item 3, item 4] Array List are equal Lets insert one more item in Array List 1 ArrayList1 = [item 1, item 2, item 3, item 4, item 5] ArrayList = [item 1, item 2, item 3, item 4] Array List are not equal

Time Complexity: O(N), where N is the length of the Array list.

Comparing two ArrayLists could be a tricky task for beginner programmers. On this page, we are going to share some of the best ways to compare two ArrayLists.

Compare ArrayLists using Equal():

You can easily compare the two ArrayLists by simply using the equals() method. This method returns a boolean value(True or False). So, if the result is true, the ArrayLists are equal; otherwise, they are not equal.

Source Code:

import java.util.ArrayList; public class ComparingArrayLists { //using equals() public static void main(String[] args) { //initializing two array lists ArrayList < String > list1 = new ArrayList < > (); ArrayList < String > list2 = new ArrayList < > (); //storing data in list 1 list1.add("Java"); list1.add("Python"); list1.add("C++"); //storing data in list 2 list2.add("Java"); list2.add("Python"); list2.add("C++"); //printing both lists System.out.println("List 1:" + list1); System.out.println("List 2:" + list2); //comparing both lists if (list1.equals(list2)) { System.out.println("Lists are equal"); } else { System.out.println("Lists are not equal"); } //adding another element to list 1 list1.add("HTML"); //comparing both lists again if (list1.equals(list2)) { System.out.println("Lists are equal"); } else { System.out.println("Lists are not equal"); } } }

Output:

run: List 1:[Java, Python, C++] List 2:[Java, Python, C++] Lists are equal Lists are not equal BUILD SUCCESSFUL (total time: 5 seconds)

Compare Arraylists using retainAll():

In case you want to compare two ArrayLists with respect to common elements, then you should be trying retainAll() method. It will return all common elements between two ArrayLists.

Source code:

import java.util.ArrayList; public class ComparingArrayLists {//using retainAll() public static void main(String[] args) { //initializing two array lists ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); //storing data in list 1 list1.add("Mike"); list1.add("Sara"); list1.add("John"); //storing data in list 2 list2.add("Mike"); list2.add("Sara"); list2.add("Diaz"); list2.add("Sam"); //printing both lists System.out.println("List 1:" + list1); System.out.println("List 2:" + list2); //it will return common elements list2.retainAll(list1); //printing common elements System.out.println("Common elements are: " + list2); } }

Output:

run: List 1:[Mike, Sara, John] List 2:[Mike, Sara, Diaz, Sam] Common elements are: [Mike, Sara] BUILD SUCCESSFUL (total time: 14 seconds)

Compare ArrayLists using contains():

An alternate way of retainAll() is to compare the elements of the ArrayLists using contains() method. The contains() method checks whether List 1 has the same element as List 2 at the same index.

Source code:

import java.util.ArrayList; import java.util.Arrays; public class ComparingArrayLists {//using contains() public static void main(String[] args) { //initializing two array lists ArrayList list1 = new ArrayList<>(Arrays.asList("Apple", "Banana", "Peach", "Apricot")); ArrayList list2 = new ArrayList<>(Arrays.asList("Cabbage", "Carrots", "Cucumber", "Banana")); //printing both lists System.out.println("List 1:" + list1); System.out.println("List 2:" + list2); //Finding similar items in both lists int i=0; while(i<list1.size() && i<list2.size()){ if(list2.contains(list1.get(i))){ System.out.println(list1.get(i) + " is in both lists"); } i++; } } }

Output:

run: List 1:[Apple, Banana, Peach, Apricot] List 2:[Cabbage, Carrots, Cucumber, Banana] Banana is in both lists BUILD SUCCESSFUL (total time: 7 seconds)

References:

  • More on Java Array List
  • Add elements to Array List

Happy Learning 🙂

FacebookTwitterRedditLinkedInTumblrPinterestVkEmail

Video liên quan

Chủ đề