• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

HELP - Stuck on a TreeMap key problem

Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic