java

How to Compare Two ArrayList in Java

In this tutorial, we are going to see how to compare two ArrayList in Java. You can compare two ArrayLists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it to the current object, if there is a match, it returns TRUE and otherwise, it returns FALSE.
 

Java Program to Compare Two ArrayList:
import java.util.ArrayList;

public class Main {
   public static void main(String[] args) {
	   
      ArrayList<String> list1 = new ArrayList<String>();
      list1.add("Java");
      list1.add("PHP");
      list1.add("Python");
      list1.add("Pascal");
	  
      ArrayList<String> list2 = new ArrayList<String>();
      list2.add("Java");
      list2.add("PHP");
      list2.add("Python");
      list2.add("Pascal");
	  
      if(list1.equals(list2))
          System.out.println("The two ArrayList are equal.");
      else
          System.out.println("The two ArrayList are not equal.");
   }
}

Output:

The two arraylists are equal.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *