I have the following:
List someObjectList (one variable inside is years already ordered as DESC by PosgreSQL)
Let us say from 2018 to 2016 years (I need it as set because many someObject contains the same years value and I need just the unique in DESC order
I put it in Stream as follows:
Set<Integer> yearsSet = someObjectList.stream()
.map(SomeObject::getYears)
.collect(Collectors.toSet())
It returns set but with ASC years like 2016, 2017, 2018 not as had in objectsList ([0].getYears()>2018, [1].getYears()> 2017...etc) at the beginning
Also I used following .sort() method in both ways:
someObjectList.stream().map(SomeObject::getYears)
.sorted(Comparator.comparing(Integer::intValue).reversed())
.collect(Collectors.toSet())
and
someObjectList.stream().map(SomeObject::getYears)
.sorted(Comparator.comparing(Integer::intValue))
.collect(Collectors.toSet())
Both did nothing (maybe I used Integer::intValue
incorrectly)?
Then I came to a solution which is ugly (here I get list but it is okey as no dublicated are received):
someObjectList.stream().map(SomeObject::getYears)
.collect(Collectors.toSet())
.stream().sorted(Comparator.comparing(Integer::intValue).reversed())
.collect(Collectors.toList())
It does the job but it is ugly. Any advise where I am doing wrong or how could I replace this "sausage" part?
I read somewhere that Maps can not be easily sorder and maybe it is applied to sets?
Thank you for your time and advise