TreeMap printout order

Discussion in 'Java' started by chunji08@gmail.com, Mar 8, 2006.

  1. Guest

    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
     
    , Mar 8, 2006
    #1
    1. Advertising

  2. Ian Pilcher Guest

    wrote:
    > 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

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Mar 8, 2006
    #2
    1. Advertising

  3. Roedy Green Guest

    On Tue, 07 Mar 2006 20:59:09 -0600, Ian Pilcher
    <> wrote, quoted or indirectly quoted someone who
    said :

    >
    >Are you using Strings as keys?


    If you use ISO format yyyy-mm-dd you can get away with string
    ordering.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 8, 2006
    #3
  4. wrote:
    > 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.

    --
    John Bollinger
     
    John C. Bollinger, Mar 8, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    0
    Views:
    309
  2. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,513
    Roedy Green
    Aug 10, 2003
  3. Mike Grace

    Can I output a printout as a tif?

    Mike Grace, Aug 27, 2003, in forum: Java
    Replies:
    1
    Views:
    381
    Mike Grace
    Aug 27, 2003
  4. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    499
    Harry
    Sep 23, 2003
  5. news.amnet.net.au
    Replies:
    2
    Views:
    9,331
    Roedy Green
    Apr 22, 2004
Loading...

Share This Page