Analog to negating .filter()

68 Views Asked by At

So I have a stream and I am trying to filter it. Every object inside that stream has one of 5 different statuses. Every item that has one specified status is gonna make that list. Then I'll use a foreach to change another attribute of those filtered objects.

The thing is: I have to do something with the objects that were filtered out of my list. So I need a way to get them too, but since there are like 5 different statuses I didn't want to put all of them inside another filter.

Is there a way of putting an if/else inside the filter or foreach? Is there a way to negate the filtering?

Cannot post code because it is proprietary.

1

There are 1 best solutions below

0
On

From the description of your problem I assume you are looking for the negation of .filter() method from Stream API. Assuming the method invocation looks as following:

stream.filter(booleanExpression)

to get the stream of elements filtered out from the invacation above use the following:

stream.filter(!booleanExpression)