How to capture the List of removed items from Java 8 Stream filtering? -


i using following expression filter list of people birthday matches criteria.

list<person> matchingpeople = people.stream() .filter(p -> datefilters.stream()     .anymatch(df ->         numofdaysbetween(p.getbirthdate(), df.getdate())  < df.getdiffrange()     ) ) .collect(collectors.tolist()); 

collectors.tolist() returns list of people matching criteria. wondering how capture list of people got removed debugging/logging purpose. 1 possible way run list through filter, inefficient. can in same filter ?

yes, can in same filter call :

 list<person> matchingpeople =       people.stream()            .filter(p -> {               if (datefilters.stream()                              .anymatch(df -> numofdaysbetween(p.getbirthdate(), df.getdate()) < df.getdiffrange() ))                {                   return true;               } else {                   //you can add here code log elements don't pass filter                   //or can add these elements external list                   return false;               }           })           .collect(collectors.tolist()); 

another alternative partition input list 2 lists based on filter predicate :

map<boolean, list<person>> partition =      people.stream()            .collect(collectors.partitioningby(p -> <same code in filter method call>)); 

Comments