Write a Java program to compare two array lists

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.*;
  public class Javacollectionexcercise {
  public static void main(String[] args) {
   ArrayList<String> Stringslist1= new ArrayList<String>();
 
        Stringslist1.add("Audi");
        Stringslist1.add("BMW");    
        Stringslist1.add("Bugatti");
        Stringslist1.add("Ford");
        Stringslist1.add("Honda");
        Stringslist1.add("Hyundai");
 
        ArrayList<String> Stringslist2= new ArrayList<String>();
        Stringslist2.add("Audi");
        Stringslist2.add("BMW");    
        Stringslist2.add("Bugatti");
        Stringslist2.add("Ford");      
 
          //Storing the comparison output in ArrayList<String>
          ArrayList<String> Stringslist3 = new ArrayList<String>();
          for (String e : Stringslist1)
             Stringslist3.add(Stringslist2.contains(e) ? "Yes" : "No");
          System.out.println(Stringslist3);
 
     }
}

Result

Write a Java program to compare two array lists
Write a Java program to compare two array lists

Leave a Comment