Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Steve
Steve
But your code only demonstrates sorting a List. What happens when you print the Set before the Collections.sort call? Of course you are sorting Strings which implement Comparable already.Originally posted by Steve Luke:
When I run similar code (see below) the Strings are sorted inside the Set - as the contract for TreeMap says they should be.
Originally posted by Campbell Ritchie:
But your code only demonstrates sorting a List. What happens when you print the Set before the Collections.sort call? Of course you are sorting Strings which implement Comparable already.
Steve
Originally posted by Steve Luke:
When I run similar code (see below) the Strings are sorted inside the Set - as the contract for TreeMap says they should be. Can you post exact code that
1) Generates the Map
2) Gets the List of Entries
3) Iterates over the List
that shows your problem?
[ August 22, 2008: Message edited by: Steve Luke ]
Originally posted by M Burke:
Once thing that is different with my TreeMap (forumMap) does not contain a String as the contained type.
Originally posted by M Burke:
But the key is a String.
Steve
Originally posted by Steve Luke:
That would be one of the first things I would double check. When you get the List of Keys, iterate over it displaying the Class of the value in the list.
As stated above, the values will be sorted in the Map. They will be sorted when the keys are pulled outs as a Set, and the Collections.sort will properly sort Strings because String implements Comparable.
I could conceive that the Set could have un-sorted data returned to the List if the Map is still being built while you do
ArrayList lKeys = new ArrayList(keys);
The Set is backed by the original Map, so changes in the Map are reflected in the Set. If these changes happen while the List is iterating the Set you are likely to see inconsistent data.
But the List will be a snap-shot of the Set, and any further changes in the Set (or Map) will not affect the List once it is made, and the Collections.sort will work on that snap shot.
Originally posted by M Burke:
There is something about my TreeMap not using Strings as data that is messing thing up.
Steve
When I use a TreeMap that contains Strings as the data
Steve
Originally posted by M Burke:
The list reads like this...
Inventory
Sales
Energy
Operations
Trucks
And it remains unchanged after the sort()
Steve