Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP - Stuck on a TreeMap key problem

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic