Get the remaining list of element after removing a particular element list out of a java list?

Supun Wijerathne :

Imagine I have a list of elements.

List<Session> sessions;

Imagine this is populated with some elements.

And I have another list which is a sub list of the above list.

List<Session> successSessions;

As name suggests, the latter is the set of success sessions out of the first list. Therefore the remaining list is the list of failedSessions.

What is an effective way to get failedSessions populated using the whole list of sessions and successSessions?

Thank You in advance.!!!

Naman :

You can do it using removeAll as:

List<Session> failedSessions = new ArrayList<>(sessions); // initialise with 'sessions'
failedSessions.removeAll(successSessions);

Guess you like

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