adaPlease adaPlease - 5 months ago 35
Java Question

Java 8 stream map to list of keys sorted by values

I have map

Map<Type, Long> countByType
and I want to have a list which has sorted (min to max) keys by their corresponding values. My try is:


however this just gives me a list of entries, how can I get a list of types, without losing the order?


You say you want to sort by value, but you don't have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort.

And you want to get the keys; use map to transform entries to keys.

List<Type> types = countByType.entrySet().stream()