Comparing Two ArrayList In Java


There are various methods available, to compare the two particular array lists by using a Java environment. It consists of the array lists, as they are same in the size and should contain with the same elements in that particular lists. Here are some process mentioned below −

  • Java equals() method

  • Java removeAll() method

  • Java retainAll() method

  • Java ArrayList.contains() method

  • Java contentEquals() method

  • java.util.ArrayList method

For this process −

  • This particular function has a single parameter value to be compared for the equality.

  • The process will return a true pop up if the array lists are equal.

Here is an example −

Input For The Operation:
ArrayList One = [1, 2, 3, 4],
ArrayList Two = [1, 2, 3, 4]
Output Is Here: Both Array Lists are equal Here
Input For The Operation :
ArrayList One = [1, 2, 3, 4],
ArrayList Two = [4, 2, 3, 1]
Output Is Here: Both Array List are not equal Here

Algorithm to Compare to Array List

In this possible algorithm, we are going to show you how to perform the process of comparing between two array list by using various methods present in a Java environment.

  • Step 1 − Start the process.

  • Step 2 − Import and declare the Java packages to run the method.

  • Step 3 − Declare a public class.

  • Step 4 − Mention the string argument.

  • Step 5 − Create And declare two array lists.

  • Step 6 − Populate the array list one.

  • Step 7 − Populate the array list two.

  • Step 8 − Display both array list.

  • Step 9 − Compare the both array lists.

  • Step 10 − If it is a true evaluation, then print the equal message.

  • Step 11 − If it is a false evaluation, block the execution process and print the not equal text.

  • Step 12 − Insert one more element in the array list one.

  • Step 13 − Display both array list.

  • Step 14 − Again compare both array lists.

  • Step 15 − Get the results.

  • Step 16 − Terminate the process.

Syntax to Compare to Array List

ArrayList<String> list1 = new ArrayList<String>();
list1.add("A");
list1.add("B");
list1.add("C");
list1.add("D");
ArrayList<String> list2 = new ArrayList<String>();
list2.add("E");
list2.add("F");
list2.add("G");
list2.add("H");
System.out.println(list2);
System.out.println(list1.equals(list2));
public class CompareTwoLists {
	List<String> listOne = Arrays.asList("b", "c", "a");
	List<String> listTwo = Arrays.asList("a", "c", "b");
	List<String> listThree = Arrays.asList("c", "a", "b");
	Collections.sort(listOne);
	Collections.sort(listTwo);
	Collections.sort(listThree);
	boolean isEqual = listOne.equals(listTwo);
	System.out.println(isEqual);
	isEqual = listOne.equals(listThree);
	System.out.println(isEqual);
}
List<String> listOfAdditionalItems = listOne.stream()
.filter(item -> !listTwo.contains(item))
.toList();
Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("c", "d"),
listOfAdditionalItems));
List<String> listOfMissingItems = listTwo.stream()
.filter(item -> !listOne.contains(item))
.toList();
Assertions.assertTrue(CollectionUtils.isEqualCollection(List.of("e", "f"),
listOfMissingItems));

In this possible syntax above, we have tried to show you how to declare and create a comparing class and get the possible outcome by comparing two particular arrays by using this method. By following these particular syntax, we are heading towards some possible approaches related to the problem statement.

Approaches to Follow

  • Approach 1 − Java program to compare two ArrayList elements by using different plugin parameters

  • Approach 2 − Java program to compare the two array lists by using java.util.ArrayList which has a single parameter

Approach 1: Compare two ArrayList Elements by Using Different Plugin Parameters

Use of Equals() Method

In this possible approach, we are going to apply the equals() method as a least interface.It can compare the specific objects to get the equal value with the particular lists. This method returns a true value if we can find that specific object in the list. Or else it returns a false value.

public class CompareTwoArrayLists {
	ArrayList<Integer> listOne = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
	ArrayList<Integer> listTwo = new ArrayList<>(Arrays.asList(1, 2, 4, 5, 6, 7));
	listOne.compareAll(listTwo);
}

Example

//Java program to compare the both list by using the equals() method
import java.util.*;
public class ARBRDDequalmethods{
   public static void main(String args[]){
      ArrayList<String> firstList=new ArrayList<String>();
      firstList.add("LADAKH");
      firstList.add("PAHALGHAM");
      firstList.add("KARGIL");
      firstList.add("GULMARG");
      System.out.println(firstList);
      List<String> secondList=new ArrayList<String>();
      secondList.add("Sonamarg");
      secondList.add("Kolkata");
      secondList.add("Dhaka");
      secondList.add("Mango");
      System.out.println(secondList);
      boolean boolval = firstList.equals(secondList);
      System.out.println(boolval);
      secondList.add("Papaya");
      boolean bool = firstList.equals(secondList);
      System.out.println(bool);
   }
}

Output

[LADAKH, PAHALGHAM, KARGIL, GULMARG]
[Sonamarg, Kolkata, Dhaka, Mango]
false
false

Use of RemoveAll() Method

In this possible approach, we are going to apply removeAll() method on a particular array list to remove all the elements from that particular list.The method overrides the method of the AbstractCollection<E>. If the process returns a true notation, the list will be changed as an outcome of the calling value.

Example

//Java program to compare the both list by using the removeAll() method
import java.util.*;
public class ARBRDD{
   public static void main(String args[]){
      ArrayList<String> firstList=new ArrayList<String>();
      firstList.add("KOLKATA");
      firstList.add("DHAKA");
      firstList.add("INDIA");
      firstList.add("BANGLADESH");
      ArrayList<String> secondList=new ArrayList<String>();
      secondList.add("CHICKEN");
      secondList.add("BEEF");
      secondList.add("PORK");
      secondList.add("MUTTON");
      secondList.removeAll(firstList);
      System.out.println(secondList);
   }
}

Output

[CHICKEN, BEEF, PORK, MUTTON]

Use of RetainAll() Method

In this possible approach, we are going to apply the retainAll() method. This method retains only the elements of a list whic is also present in an another list as a common value. This process also overrides the value of the AbstarctCollection<E> class.

Example

//Java program to compare the both list by using the retainAll() method
import java.util.*;
public class ARBRDD{
   public static void main(String args[]){
      ArrayList<Integer> firstList=new ArrayList<Integer>(Arrays.asList(12, 4, 67, 90, 34));
      System.out.println("First array list is here: ");
      System.out.println(firstList);
      List<Integer> secondList=new ArrayList<Integer>(Arrays.asList(12, 4, 67, 0, 34));
      System.out.println("Second array list is here: ");
      System.out.println(secondList);
      firstList.removeAll(secondList);
      System.out.println("Un-common element of the first list: ");
      System.out.println(firstList);
   }
}

Output

First array list is here:
[12, 4, 67, 90, 34]
Second array list is here:
[12, 4, 67, 0, 34]
Un-common element of the first list:
[90]

Use of ArrayList.contains() Method

In this possible approach, we are going to apply the ArrayList.Contains() method, which compares the elements present in two different array list. If the value mathed by the operation we will get a true value or else the whole method will return false.

Example

//Java program to compare the both list by using the ArrayList.contains() method
import java.util.*;
public class ARBRDD{
   public static void main(String args[]){
      ArrayList<String> firstList=new ArrayList<String>(Arrays.asList("A", "B", "O", "N", "I"));
      System.out.println("First array list is here: ");
      System.out.println(firstList);
      List<String> secondList=new ArrayList<String>(Arrays.asList("R", "U", "D", "R", "A"));
      System.out.println("Second array list is here: ");
      System.out.println(secondList);
      secondList.retainAll(firstList);
      System.out.println("Common elements in both list: ");
      System.out.println(secondList);
   }
}

Output

First array list is here:
[A, B, O, N, I]
Second array list is here:
[R, U, D, R, A]
Common elements in both list:
[A]

Use of ContentEquals() Method

In this possible approach, we are going to apply the the method of contentEquals(). It compares the string classes with a buffer class. Return a boolean value that belongs to that particular string class.

Example

//Java program to compare the both list by using the ArrayList.contains() method
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ARBRDD{
   public static boolean compareList(ArrayList ls1, ArrayList ls2){
      return ls1.toString().contentEquals(ls2.toString())?true:false;
   }
   public static void main(String[] args){
      ArrayList<String> firstList = new ArrayList<String>(Arrays.asList("Java", "Python", "Ruby", "Go"));
      ArrayList<String> secondList = new ArrayList<String>(Arrays.asList("Java", "Python", "Ruby", "Go"));
      System.out.println("When Lists are same the same here: "+compareList(firstList, secondList));
      secondList.add("C++");
      System.out.println("When Lists are not same here in this process: "+compareList(firstList, secondList));
   }
}

Output

When Lists are same the same here: true
When Lists are not same here in this process: false

Approach 2: Compare the two Array Lists With Single Parameter

Use of the Java.util.ArrayList Method

In this possible approach,we are going to apply the java.util.ArrayList method. This is mainly used to compare the two array lists. Both array list will be same in the size and element count should be equal in those list. The time complexity of this process is O(N).

boolean equals(Object o)

Example

//Java program to compare the two array lists by using java.util.ArrayList which has a single parameter
import java.util.ArrayList;
public class ARBRDD{
   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 here
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 here

Conclusion

To find the common elements from the two array list, it is more convenient to use the retain all method on those particular similar array list. Today in this article we have learned about the process to compare two similar array lists by using various methods present in Java. By using the above mentioned syntax and algorith, we have developed some Java codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements