TreeMap printout order

C

chunji08

DATAs are being put in a TreeMap, how could I make sure that these
datas are being print out in the order they are read in ?

For example, I have datas as:
"07/20/2005", "NY",
"06/01/2005", "MA,
"01/01/2006", "CA".

The output should be the same as above, NOT
"01/01/2006", "CA".
"06/01/2005", "MA,
"07/20/2005", "NY",


I have tried different ways, no good. Help if you could !


cji
 
I

Ian Pilcher

DATAs are being put in a TreeMap, how could I make sure that these
datas are being print out in the order they are read in ?

For example, I have datas as:
"07/20/2005", "NY",
"06/01/2005", "MA,
"01/01/2006", "CA".

The output should be the same as above, NOT
"01/01/2006", "CA".
"06/01/2005", "MA,
"07/20/2005", "NY",


I have tried different ways, no good. Help if you could !

Are you using Strings as keys? If so, the map will be sorted by the
lexical value of the Strings -- "01" < "06" < "07". You should be using
a class that handles dates more intelligently, such as Calendar.

HTH
 
J

John C. Bollinger

DATAs are being put in a TreeMap, how could I make sure that these
datas are being print out in the order they are read in ?

For example, I have datas as:
"07/20/2005", "NY",
"06/01/2005", "MA,
"01/01/2006", "CA".

The output should be the same as above, NOT
"01/01/2006", "CA".
"06/01/2005", "MA,
"07/20/2005", "NY",


I have tried different ways, no good. Help if you could !

TreeMap will return keys, values, and entries in the relevant sort order
for the keys. If you want a Map implementation that returns all of
those in insertion order instead then you should consider LinkedHashMap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top