I want to get the below
1. common elements between two list.
2. Elements in ListB which is not available in ListA
3. Elements in ListA which is not available in ListB
List<String> fruitsListA = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Grapes");
List<String> fruitsListACopy = fruitsListA;
List<String> fruitsListB = new ArrayList<String>();
fruits.add("Strawberry");
fruits.add("Cranberry");
fruits.add("Orange");
fruits.add("Apple");
List<String> fruitsListBCopy = fruitsListB;
1. common elements between two list
fruitsListACopy.retainAll(fruitsListB);
2. Elements in ListB which is not available in ListA
fruitsListB.removeAll(fruitsListA);
3. Elements in ListA which is not available in ListB
fruitsListA.removeAll(fruitsListBCopy);
case 1 : Im able to get the common items in both the list. So If I print fruitsListACopy it gives ['Apple']
case 2 : I could see fruitsListA got modified automatically I mean instead of having all the items ['Apple','Banana','Grapes'] it has only common item ['Apple'] .
case 3: I could see fruitsListB got modified automatically I mean instead of having all the items ['Strawberry','Cranberry','Orange','Apple'] it has only
['Strawberry','Cranberry','Orange'] with the removal of coomon item which is Apple.
I could even see fruitsListA got modified automatically I mean instead of having all the items ['Apple','Banana','Grapes'] it has only common item
['Apple'] .
The changes done in the copied list gets reflects in original list when I use removeAll and retainAll methods.
Is there any other best way to meet this need?