Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting Map by custume Object field ID

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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()));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading