javasortinglinkedhashmap

Sorting LinkedHashMap based on its values


How can I sort a LinkedHashMap<String, Integer> based on its values?.

I need to sort it based on the values which are Integers.


Solution

  • This is now quite a bit easier with Java 8 streams: you don't need the intermediate map to sort:

    map.entrySet().stream()
        .sorted(Map.Entry.comparingByValue())
        .forEach(entry -> ... );