I have a list containing 3 objects, sorted by modification date, such as:
{id=1, name=apple, type=fruit, modificationDate=2019-09-02}
{id=2, name=potato, type=vegetable, modificationDate=2019-06-12}
{id=3, name=dog, type=animal, modificationDate=2018-12-22}
What I need to do is to filter those items in such way: an object of type animal will be always pass to new list, but only one item of type fruit and vegetable (the one with most recent date of modification) will be passed, so the result list would be the following:
{id=1, name=apple, type=fruit, modificationDate=2019-09-02}
{id=3, name=dog, type=animal, modificationDate=2018-12-22}
I tried to combine findFirst() and filter() on stream operation, altough, I only managed to make it work one after another, not as 'or' conditions.
It works with such a code:
List<Item> g = list.stream().filter(f -> f.getType() == animal).collect(Collectors.toList());
Item h = list.stream().filter(f -> f.getType() != animal).findFirst().get();
g.add(h);
But it's extremaly ugly solution, so I'm looking for something more elegant.
Any help appreaciated!
PS. The list will always contain only 3 items, 1 of type animal, which should stay and 2 items of different types, sorted descending by modification date.