This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
My selectMap() method returns a TreeMap. I've confirmed that the keys are being placed into the TreeMap *in order* (not that that should matter). When I then retrieve the keySet and try to iterate over them, I find that their "natural order" seems to be wacky.
Starting at the bottom, read the numbers from right to left. (imagine you're reading them in a mirror) 01 11 1 2 21 etc...
The same thing happens when I try to get values() to get a Collection view. From the API: But in fact, it's in the same order as shown above (only the output is the 'value' part of the TreeMap and not the key)
What is going on? Is the "natural order" of Strings (as implemented by String's default Comparator) like this?
I think not, but what else could be happening? [ April 05, 2003: Message edited by: Mike Curwen ]
Here's the full code:Two things of note here. //2 The extra "" was in fact turning it back into a String. If I removed the "", then I'd get a sorted result in DESCENDING order.
The real problem.... //1 DUH! It's a HashMap again! This is the type of thing that happens a lot when you 'improve' code, right? (the method didn't used to take a boolean 'sorted' parameter). [ April 06, 2003: Message edited by: Mike Curwen ] [ April 06, 2003: Message edited by: Mike Curwen ]
Yup. I was checking at the top of my method to see if I should make a HashMap or TreeMap, but then later on (at //2) I go ahead and make it a HashMap. [ April 06, 2003: Message edited by: Mike Curwen ]