1

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?

Alagammal P
  • 829
  • 5
  • 19
  • 43
  • The copied variables are references. They are pointing to the original lists. So if you make changes in the copied variables they will update the original list also. – Pooja Arora Apr 03 '17 at 09:37
  • You need to do a so called `deep copy` of your list(s) instead of `shallow copy` which are you doing now, because only references are contained in your lists. – alexandrum Apr 03 '17 at 09:43

2 Answers2

1

Creating a new instance instead of assigning the same object to another reference.

For example, you can replace :

    List<String> fruitsListACopy = fruitsListA;

by

    List<String> fruitsListACopy = new ArrayList<>(fruitsListA);

If you don't want side effects between operations on the copied lists, you have to create a new copy of the original list before each operation on it.

Then you have to call retainAll() and removeAll() on the copied lists and provide as parameter to the methods the original lists.

Here is a sample code to illustrate :

public static void main(String[] args) {
    List<String> fruitsListA = new ArrayList<String>();
    fruitsListA.add("Apple");
    fruitsListA.add("Banana");
    fruitsListA.add("Grapes");

    List<String> fruitsListB = new ArrayList<String>();
    fruitsListB.add("Strawberry");
    fruitsListB.add("Cranberry");
    fruitsListB.add("Orange");
    fruitsListB.add("Apple");

    // copy for A
    List<String> fruitsListACopy = new ArrayList<>(fruitsListA);
    fruitsListACopy.retainAll(fruitsListB);
    // handle the result of fruitsListACopy
    System.out.println("fruitsListACopy="+fruitsListACopy);

    // copy for B
    List<String> fruitsListBCopy = new ArrayList<>(fruitsListB);
    fruitsListBCopy.removeAll(fruitsListA);
    // handle the result of fruitsListBCopy
    System.out.println("fruitsListBCopy="+fruitsListBCopy);

    // copy for A
    fruitsListACopy = new ArrayList<>(fruitsListA);
    fruitsListACopy.removeAll(fruitsListB);
    // handle the result of fruitsListACopy
    System.out.println("fruitsListACopy="+fruitsListACopy);
}

Output :

fruitsListACopy=[Apple]

fruitsListBCopy=[Strawberry, Cranberry, Orange]

fruitsListACopy=[Banana, Grapes]

Community
  • 1
  • 1
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

I could even see fruitsListA got modified automatically

When you make a copy like yhis

List<String> fruitsListACopy = fruitsListA;

You do not copy the list. Instead, you make a reference to the existing list. Hence, all modifications to the "copy" reflect on the original.

To make a copy, call constructor again:

List<String> fruitsListACopy = new ArrayList<String>(fruitsListA);
Alagammal P
  • 829
  • 5
  • 19
  • 43
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523