Java streams filtering

Viewed 156

Hello I have a list of dtos which have flag is deleted. I need to filter them and return one.

The logic is that if there is two items in the list and one of them is deleted I fetch non deleted one, but if there is only one item and deleted then I return it.

So basically order is not deleted > deleted > new item.

List<Item> existingItems = service.getItems();
existingItems.stream().filter(e -> !e.getIsDeleted()).findAny().orElse(new Item());

How can I modify this Stream pipeline to implement the required logic?

3 Answers
Related