This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes HELP - Stuck on a TreeMap key problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "HELP - Stuck on a TreeMap key problem" Watch "HELP - Stuck on a TreeMap key problem" New topic
Author

HELP - Stuck on a TreeMap key problem

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Im am using a TreeMap to store data coming out of a database that MUST be in order (why I chose the TreeMap) but then to iterate through the data I have to use the keySet() method which returns a Set, then I am using the iterator() method to get my Iterator - The set blows the order right out of the water..........Im as stuck as stuck can be here & my head hurts from beating it on the desk! Anyone out there have a suggestion??
What I am storing is a unique object name (built with a counter) & then the object as the value. I am then creating pdf files on the fly that must come out of the printer with the nearest due date on the top of the pile. Its actually a whole lot more complicated than this but I wont bore you with the details, lets just say Im running through 3 company names & creating 16 different pdfs in one shot...all based on certain criteria. I cant use any of the TreeMap methods for getting the value via a key because I have no idea how many it may hold.....any & all help would save me a few years of grey hairs! Thanks
[ November 07, 2002: Message edited by: DC Dalton ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
When using TreeSet, any iterators associated with the keySet() or entrySet() should be in sorted order. Try this to test:

Assuming you see the output in order, your TreeSet implementation is working fine. So the problem is probably elsewhere. Are you using a Comparator? Then check the code of the compare() method. Or if you're using Comparable keys, check the compareTo() code.
If you're using a preexisting Comparable class (like String) be aware that "natural order" may not be exactly what you expect. For example numbers like "8", "9", "10", "11" are sorted alphabetically as "10, "11", "8", "9". If that's not what you want, you may need to make a Comparator to correct the problem.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: HELP - Stuck on a TreeMap key problem
 
Similar Threads
Problem creating new objects
Iterator
Real test !
Using TreeMap class
HashMap