How can I return the difference between two lists?

Rory Lester picture Rory Lester · Nov 18, 2014 · Viewed 142.2k times · Source

I have two array lists e.g.

List<Date> a;
contains : 10/10/2014, 10/11/2016

List<Date> b;
contains : 10/10/2016

How can i do a check between list a and b so the value that is missing in b is returned?e.g. 10/10/2014

Answer

Pablo Santa Cruz picture Pablo Santa Cruz · Nov 18, 2014

You can convert them to Set collections, and perform a set difference operation on them.

Like this:

Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);