My first question here, I have a Map<custumeObject, String> I need to sort the map based on the custumeObject field Id I already try to sort by key but result return messy
map.entrySet().stream().sorted(Map.Entry.comparingByKey())
My Costume Object
Class Person {
private Long id;
}
I need to sort the Map by Customer Id I search for solution but I can`t find.Any help will be welcomed.
>Solution :
You can use the Comparator#comparing(Function) method to extract a comparable key out of your Entry objects keys:
map.entrySet().stream().sorted(Comparator.comparing(entry -> entry.getKey().getId()));