I am new to java .
I have 2 ArrayLists of Strings
List<String> a= [2,14]
List<String> b= [2,3,4,5]
I want two new ArrayLists
1) List has the value which is in b but not in a
List<String> c= [3,4,5]
2) List has the value a but not in b
List<String> d=[14]
I tried:
List<String> c = new ArrayList<String>(b);
c.removeAll(a);
System.out.println("c::::::::::::::::::::::::::::::"+c); // 2,3,4,5
which is not removing the values of List a
Complete Code
public static void updatePartyType(List<String> oldPartyKeys, List<String> newPartyKeys, String customerCode) {
System.out.println("oldPartyKeys--->"+oldPartyKeys);// 2,14
System.out.println("newPartyKeys--->"+newPartyKeys); // 2,3,4,5
System.out.println("oldPartyKeys class --->"+oldPartyKeys.getClass());// class java.util.ArrayList
List<String> newlySelectedPartyKeys = new ArrayList<String>(newPartyKeys);
newlySelectedPartyKeys.removeAll(oldPartyKeys);
System.out.println("newlySelectedPartyKeys::::::::::::::::::::::::::::"+newlySelectedPartyKeys);

You're really proposing set operations more than list operations - in which case you'd be better off using a HashSet than an ArrayList. Then you could use Collection<E>.removeAll:
Set<String> a = ...;
Set<String> b = ...;
Set<String> c = new HashSet<String>(b);
c.removeAll(a);
Set<String> d = new HashSet<String>(a);
d.removeAll(b);
(This will work for ArrayList as well as HashSet - I've only changed to using sets because it's a more appropriate type when you want set-based operations.)
Or better, use Guava's Sets.difference method:
Set<String> a = ...;
Set<String> b = ...;
Set<String> c = Sets.difference(b, a);
Set<String> d = Sets.difference(a, b);
This will create views on the differences - so changes to the original sets will be reflected in the views. You can effectively take a snapshot of a view by creating a new HashSet:
Set<String> snapshot = new HashSet<String>(c);
See more on this question at Stackoverflow