This is a very basic question, I’m just not that good with Java. I have a Map and I want to get a list or something of the keys in sorted order so I can iterate over them.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a
TreeMap, which is an implementation of theSortedMapinterface. It presents its keys in sorted order.If you are working with another Map implementation that isn’t sorted as you like, you can pass it to the constructor of
TreeMapto create a new map with sorted keys.A
TreeMapworks with any type of key that implements theComparableinterface, putting them in their ‘natural’ order. For keys that aren’tComparable, or whose natural ordering isn’t what you need, you can implement your ownComparatorand specify that in the constructor.