I have an Answer class and an User class.
Answer has a getUser()
and User has a getPoints()
From a list of Answer I would like to get a HashSet of User sorted by points. I tried following:
Set<User> collectSet = list.stream().map(Answer::getUser)
.sorted(Comparator.comparing(User::getPoints))
.collect(Collectors.toCollection(HashSet::new));
collectSet.forEach(a -> System.out.println(a.toString()));
Unfortunately this doesn't seem to preserve the order. The output is always different.
Interesting is that the same example with list does work correctly
List<User> collectList = list.stream().map(Answer::getUser)
.sorted(Comparator.comparing(User::getPoints))
.collect(Collectors.toList());
collectList.forEach(a -> System.out.println(a.toString()));
What am I doing wrong?