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..


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. import java.util.*;
  2. public class Javacollectionexcercise {
  3. public static void main(String[] args) {
  4. ArrayList<String> Stringslist1= new ArrayList<String>();
  5.  
  6. Stringslist1.add("Audi");
  7. Stringslist1.add("BMW");
  8. Stringslist1.add("Bugatti");
  9. Stringslist1.add("Ford");
  10. Stringslist1.add("Honda");
  11. Stringslist1.add("Hyundai");
  12.  
  13. ArrayList<String> Stringslist2= new ArrayList<String>();
  14. Stringslist2.add("Audi");
  15. Stringslist2.add("BMW");
  16. Stringslist2.add("Bugatti");
  17. Stringslist2.add("Ford");
  18.  
  19. //Storing the comparison output in ArrayList<String>
  20. ArrayList<String> Stringslist3 = new ArrayList<String>();
  21. for (String e : Stringslist1)
  22. Stringslist3.add(Stringslist2.contains(e) ? "Yes" : "No");
  23. System.out.println(Stringslist3);
  24.  
  25. }
  26. }

Result

java techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.