How to compare two ArrayList for equality in Java?

Java 8Object Oriented ProgrammingProgramming

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

Example

 Live Demo

import java.util.ArrayList;

public class ComparingList {
   public static void main(String[] args) {
      ArrayList<String> list1 = new ArrayList<String>();
      list1.add("JavaFx");
      list1.add("Java");
      list1.add("WebGL");
      list1.add("OpenCV");
      ArrayList<String> list2 = new ArrayList<String>();
      list2.add("JavaFx");
      list2.add("Java");
      list2.add("WebGL");
      list2.add("OpenCV");
      System.out.println(list2);
      System.out.println(list1.equals(list2));
   }
}
raja
Published on 17-Jan-2018 06:32:41
Advertisements