Compare two List

Sathish David  Kumar N
Sathish David  Kumar N used Ask the Experts™
on
Hi ,

I want to compare two list values  How can i compare without for  loop !!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what is this i didnt get ur answer !!
there is nothing related my question Mr.xpertoPC
a_b
Top Expert 2009

Commented:
How do you want to compare them? List A and List B?
-> check whether all elements in list A are list ?
-> elements in list A not in list B?
-> elements in list B not in list A?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

a_b
Top Expert 2009

Commented:
Here is a small code snippet that you can use -

ArrayList<String> a = new ArrayList<String>();
ArrayList<String> b = new ArrayList<String>();
a.removeAll(b); // This removes all elemets in a that are present in B so you get the difference
Hi A_b
Ya i want to check wether   a ArrayList having values of  b ArrayList  if there means i want to store in new array list
a_b
Top Expert 2009

Commented:
Write your logic using the code snippet above, that should solve your problem easily.
List a=new ArrayList();
            List b=new ArrayList();
            List c=new ArrayList();
            a.add("1");
            a.add("2");
            a.add("3");
            a.add("4");
            a.add("5");a.add("6");
            b.add("1");
            b.add("7");
            b.add("8");
            b.add("4");
            b.add("5");b.add("6");

i want to store same values of  a,b arraylist  in c
how can we do with out  for loop
Top Expert 2009
Commented:
Make a copy of a as a_temp;

a_temp.removeAll(b);
a.removeAll(a_temp);

Now a will have all the values common to a and b. Copy a over to c.
But a  arraylist value are dleted as per ur code !!
so i add all contant to c arraylist and remove from a _temp arraylist

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial