Sorting by property in Java 8 stream
Solution 1:
What you want is Comparator#comparing
:
userMap.values().stream()
.sorted(Comparator.comparing(User::getName, UserNameComparator.INSTANCE))
.collect(Collectors.toList());
For the second part of your question, you would just use
Comparator.comparing(
u->u.getProfile().getUsername(),
UserNameComparator.INSTANCE
)