Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting by property in Java 8 stream

Oh, those tricky Java 8 streams with lambdas. They are very powerful, yet the intricacies take a bit to wrap one's header around it all.

Let's say I have a User type with a property User.getName(). Let's say I have a map of those users Map<String, User> associated with names (login usernames, for example). Let's further say I have an instance of a comparator UserNameComparator.INSTANCE to sort usernames (perhaps with fancy collators and such).

So how do I get a list of the users in the map, sorted by username? I can ignore the map keys and do this:

return userMap.values()     .stream()     .sorted((u1, u2) -> {       return UserNameComparator.INSTANCE.compare(u1.getName(), u2.getName());     })     .collect(Collectors.toList()); 

But that line where I have to extract the name to use the UserNameComparator.INSTANCE seems like too much manual work. Is there any way I can simply supply User::getName as some mapping function, just for the sorting, and still get the User instances back in the collected list?

Bonus: What if the thing I wanted to sort on were two levels deep, such as User.getProfile().getUsername()?

like image 389
Garret Wilson Avatar asked Oct 26 '14 00:10

Garret Wilson


People also ask

How to sort a stream in Java 8?

On this page we will provide java 8 Stream sorted () example. We can sort the stream in natural ordering as well as ordering provided by Comparator. In java 8 Comparator can be instantiated using lambda expression. We can also reverse the natural ordering as well as ordering provided by Comparator.

How do you sort a list of elements in Java?

Sorting a List using Stream sorted () method Stream sorted () returns a stream of elements sorted according to natural order. Stream sorted () method is overloaded to take a Comparator that can sort the Stream of elements in a custom order as provided by the Comparator.

How to sort the stream of elements in a list?

Stream sorted () method is overloaded to take a Comparator that can sort the Stream of elements in a custom order as provided by the Comparator. 3. Sorting List using lambda expression and Stream sorted () method 4. Sorting List using lambda expression with Collections.sort ()

What is the syntax of sorted() method in Java?

Find the syntax of sorted () method. 1. sorted (): It sorts the elements of stream using natural ordering. The element class must implement Comparable interface. 2. sorted (Comparator<? super T> comparator): Here we create an instance of Comparator using lambda expression. We can sort the stream elements in ascending and descending order.


1 Answers

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 ) 
like image 151
Misha Avatar answered Sep 19 '22 17:09

Misha