0

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);
5
  • 3
    Is the ordering important? These are set-based operations more than list-based ones. Commented Jan 1, 2014 at 10:06
  • it should be string list Commented Jan 1, 2014 at 10:07
  • 2
    Please edit the question to be a short but complete program - you've only given pseudo-code for the list initialization at the moment. Commented Jan 1, 2014 at 10:29
  • 1
    The code you've been trying should work which means the problem is elsewhere. You can go to my answer and see how it's done using your provided example using the same method you've tried. Commented Jan 1, 2014 at 10:38
  • 1
    That's still not complete code. Complete code is code which we can copy into a text editor, compile and run. Commented Jan 1, 2014 at 11:20

5 Answers 5

6

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

5 Comments

@monda I've added an example
@monda: "not working" doesn't give us any significant information. What happened when you tried it? It should be fine.
@monda, I've added an example of a static "main" function in my code. You do know how to run a "main" function? Simple drop my code in, import "List" and "ArrayList" (your IDE should mark this for you really...) and follow the comments in the code.
@Mureinik :Updated my question
@monda Maybe you only have Java 6. This answer requires Java 7. Try putting String inside each pair of <>.
2

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

}

Comments

2

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.

4 Comments

Beware that Sets.difference returns a view, not a new Set
@RC.: It returns a new Set instance - it's a view, but it's still a new set (SetView implements Set). It's not an independent set, but you could create one easily enough.
@JonSkeet I know but you can run into issues if you use the view directly (like adding something to it)
@RC.: My point is that claiming it's "not a new set" is misleading IMO. I've now mentioned that it's a view in my answer though.
1

addAll elements, then removeAll elements that appears in a.

The time complexity is O(n).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.