Tuesday, 16 April 2013

Comparing two arraylist in Java

import java.util.ArrayList;
import java.util.List;

public class CompareList {
 public static void main(String[] args) {

  List list = new ArrayList();
  list.add("Arun");
  list.add("Jack");
  list.add("Sonu");
  list.add("Sandeep");
  list.add("Aryan");

  List list1 = new ArrayList();
  list1.add("Arun");
  list1.add("Sandeep");
  list1.add("Jack Sparrow");
  list1.add("Aryan");

  for (int i = 0; i < list.size(); i++) {
   for (int j = 0; j < list1.size(); j++) {

    if (list.get(i).equals(list1.get(j))) {
     System.out.print(list1.get(j) + ","); // print all name find in both list
    }
   }
  }
 }
}
output........
Arun,Sandeep,Aryan

No comments:

Post a Comment