• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

HELP - Stuck on a TreeMap key problem  RSS feed

 
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 ]
 
Wanderer
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.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!