How can I sort a List
of Map
using Java 8? The map contains a key called last_name
, and the value associated wit
Steps to sort a Map in Java 8.
1]Convert a Map into a Stream
2]Sort it
3]Collect and return a new LinkedHashMap (keep the order)
Map result = map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));