Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return the difference between two lists?

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

like image 567
Rory Lester Avatar asked Nov 18 '14 19:11

Rory Lester


People also ask

How can I compare two lists in Python and return differences?

Method 6: Use symmetric_difference to Find the Difference Between Two Lists in Python. The elements that are either in the first set or the second set are returned using the symmetric_difference() technique. The intersection, unlike the shared items of the two sets, is not returned by this technique.

How do I compare two lists in Java and get differences?

Using the Java List API. We can create a copy of one list and then remove all the elements common with the other using the List method removeAll(): List<String> differences = new ArrayList<>(listOne); differences. removeAll(listTwo); assertEquals(2, differences.

Can you subtract two lists in Python?

Use Numpy to Subtract Two Python Lists One of the methods that numpy provides is the subtract() method. The method takes two numpy array s as input and provides element-wise subtractions between the two lists.


2 Answers

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); 
like image 198
Pablo Santa Cruz Avatar answered Oct 23 '22 14:10

Pablo Santa Cruz


If you only want find missing values in b, you can do:

List toReturn = new ArrayList(a); toReturn.removeAll(b);  return toReturn; 

If you want to find out values which are present in either list you can execute upper code twice. With changed lists.

like image 38
Denis Lukenich Avatar answered Oct 23 '22 15:10

Denis Lukenich