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.
-
Use a
TreeMap, which is an implementation of theSortedMapinterface. It presents its keys in sorted order.Map<String, Object> map = new TreeMap<String, Object>(); /* Add entries to the map in any order. */ ... /* Now, iterate over the map's contents, sorted by key. */ for (Map.Entry<String, ?> entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); }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.void process(Map<String, Object> original) { Map<String, Object> copy = new TreeMap<String, Object>(original); /* Now use "copy", which will have keys in sorted order. */ ... }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.Bialecki : The code I'm consuming gives me a Map object, so how do I then convert it into a TreeMap or use a TreeMap to do the sorting?Daniel Lew : You can create the TreeMap using a constructor whose parameter is any Map. Also, congratulations erickson (I assume, since you're only 5 rep away from 10k).Paul Tomblin : Ding! erickson levels up.erickson : Yea! And I got my silver "Java" badge the other day too.Steve Kuo : You would better off sorting keys (place in a TreeSet) then calling get on the original Map. Creating a new TreeMap results in everything getting rehashed.erickson : Steve, that's incorrect. No hashing occurs with a TreeMap; it's a red-black tree. Anyway, a TreeSet delegates to an internal TreeMap, so creating a TreeSet, iterating over its keys, and using them to perform an O(log(n)) lookup in another tree would be confusing and inefficient. -
You have several options. Listed in order of preference:
- Use a
SortedMap:
SortedMap<whatever> myNewMap = new TreeMap<whatever>(myOldMap);
This is vastly preferable if you want to iterate more than once. It keeps the keys sorted so you don't have to sort them before iterating. - There is no #2.
- There is no #3, either.
SortedSet<whatever> keys = new TreeSet<whatever>(myMap.keySet());List<whatever> keys = new ArrayList<whatever>(myMap.keySet()); Collections.sort(keys);
The last two will get you what you want, but should only be used if you only want to iterate once and then forget the whole thing.
David Zaslavsky : In step #4, you could also create a TreeSet (a sorted set) instead of a list, which saves you from making an explicit call to sort().Michael Myers : @David: I did think of that, but for some reason I forgot that it was possible to iterate over a Set. That should really be #3 then.Michael Myers : Actually, I put it at #4 because it also requires sorting every time.Johannes Schaub - litb : hmm i like the #4 . why does it require more sorting than #1 ? i would have thought it was equally goodMichael Myers : @litb: If you're dumping the map into a TreeMap every time you want to iterate over it, then you're right, they are the same.erickson : #4 isn't as good as #1 if you need the values from the map too. Iterating over entries saves the extra lookup for the value. - Use a
-
You can create a sorted collection when iterating but it make more sense to have a sorted map in the first place. (As has already been suggested)
All the same, here is how you do it.
Map<String, Object> map; for(String key: new TreeSet<String>(map.keySet()) { // accessed in sorted order. }
0 comments:
Post a Comment