Compare two lists of string using java stream

Muhammad Naeem Shahzad :

I have two lists A and B. both have millions of elements. I want to compare and get all elements those are in list A but not in list B. Below is inefficient way to get elements.

   if (!B.containsAll(A)) {
        for (Integer id : A) {
            if (!B.contains(id)) {
                System.out.println(id);
            }
        }
    }

I looking for an efficient way with or without streams to get elements

help is appreciated in this regards.

Thanks

Guy :

You don't need to compare

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

And if you don't mind loosing the original list data

a.removeAll(b);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80028&siteId=1