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.
You can use Stream.concat to join the two streams of Item one having type animal and the other of non-animal type. Then filter and sort the stream having the non-animal type Item in descending order of modificationDate and take the first one by putting a limit(1).
Then finally when the Stream is merged you sort it again, this time between the the stream of Item of animal type and stream of Item of non-animal type:
Comparator<Item> comp = Comparator.comparing(Item::getModificationDate, Comparator.reverseOrder());
List<Item> sorted = Stream.concat(list.stream()
.filter(f -> "animal".equals(f.getType())),
list.stream()
.filter(f -> !"animal".equals(f.getType()))
.sorted(comp)
.limit(1))
.sorted(comp)
.collect(Collectors.toList());
System.out.println(sorted);
I have assumed that modificationDate is a LocalDateTime field, otherwise if it is a String then the sorting will happen in the lexical order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With