monda
monda

Reputation: 3915

Java : Create new ArrayLists based on some condition from existing ArrayList

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);

Upvotes: 0

Views: 3115

Answers (5)

Reut Sharabani
Reut Sharabani

Reputation: 31339

take a look at the addAll() and removeAll() in ArrayList

now you need b\a which is b.removeAll(a) and a\b which is a.removeAll(b)

A working example (this is for those who are new to Java, so it's verbose):

public static void main(String[] args) {
    // first we want to create the lists

    // create list a
    List<String> a = new ArrayList<String>();
    // add members to a
    a.add("2");
    a.add("14");
    // create list b
    List<String> b = new ArrayList<String>();
    // add members to b
    b.add("2");
    b.add("3");
    b.add("4");
    b.add("5");

    // create a list in which we store the "filtered" list - duplicated from
    // a
    List<String> aMinusB = new ArrayList<>(a);
    // "filter" using "removeAll" and giving the list b as the argument
    aMinusB.removeAll(b);
    System.out.println("A minus b:");
    // this is short for
    // "iterate over the entire list, naming the currently iterated node s"
    for (String s : aMinusB) {
        System.out.println(s);
    }

    // duplicate list b in the same manner as above
    List<String> bMinusA = new ArrayList<>(b);
    // "filter" using "removeAll" and giving the list a as the argument in
    // the same manner as above
    bMinusA.removeAll(a);
    System.out.println("B minus a:");
    // this is short for
    // "iterate over the entire list, naming the currently iterated node s"
    // in the same manner as above
    for (String s : bMinusA) {
        System.out.println(s);
    }

}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502246

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);

Upvotes: 6

Mureinik
Mureinik

Reputation: 311853

This can be done by using the removeAll method:

List<String> c = new ArrayList<>(b);
c.removeAll(a);

List<String> d = new ArrayList<>(a);
d.removeAll(b);

Upvotes: 3

Juvanis
Juvanis

Reputation: 25950

Convert your lists to Set instances. Then you can easily find the difference of sets by your own implementation or using a 3rd party library like Google Guava which has Sets.difference(), for example.

Upvotes: 2

Maroun
Maroun

Reputation: 95978

addAll elements, then removeAll elements that appears in a.

The time complexity is O(n).

Upvotes: 1

Related Questions