ordering of items in a treemap

Discussion in 'Java' started by angelochen960@gmail.com, Apr 7, 2008.

  1. Guest

    Hi,

    I have code:

    TreeMap<String, String> shoeSize = new TreeMap<String, String>();

    shoeSize.put("8", "8");
    shoeSize.put("8.5", "8.5");
    shoeSize.put("10", "10");
    System.out.println(shoeSize.toString());

    I got:

    {10=10, 8=8, 8.5=8.5}

    I'd like to get the order same inserted:

    {8=8, 8.5=8.5, 10=10}

    any idea? thanks
     
    , Apr 7, 2008
    #1
    1. Advertising

  2. Guest

    , Apr 7, 2008
    #2
    1. Advertising

  3. Donkey Hot Guest

    "" <> wrote in news:4d216ee6-
    :

    > Hi,
    >
    > I have code:
    >
    > TreeMap<String, String> shoeSize = new TreeMap<String, String>();
    >
    > shoeSize.put("8", "8");
    > shoeSize.put("8.5", "8.5");
    > shoeSize.put("10", "10");
    > System.out.println(shoeSize.toString());
    >
    > I got:
    >
    > {10=10, 8=8, 8.5=8.5}
    >
    > I'd like to get the order same inserted:
    >
    > {8=8, 8.5=8.5, 10=10}
    >
    > any idea? thanks
    >


    Why you keep the key as a String, as the key value seems to be a Float or
    something?

    Float would compare as you want, but a String of course not.

    Make it a TreeMap<Float, String> shoeSize
     
    Donkey Hot, Apr 7, 2008
    #3
  4. voorth Guest

    On Apr 8, 10:22 am, Andrea Francia ; they'll work just fine without
    that.
    >
    > If you don't subclass the TreeMap how you can create a Comparator that
    > compare keys in insertion order?
    >
    > --
    > Andrea Franciahttp://www.andreafrancia.it/


    For insertion order, you use LinkedHashMap instead of TreeMap.

    I think the OP just wanted "natural" ordering, however. You have to
    write a Comparator for that.

    Henk
     
    voorth, Apr 8, 2008
    #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. =?Utf-8?B?RGlnZ2xlcg==?=

    Ordering items in ListBox

    =?Utf-8?B?RGlnZ2xlcg==?=, May 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    517
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    May 4, 2004
  2. Stimp

    re-ordering ListBox items

    Stimp, Nov 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,106
    Stimp
    Nov 19, 2004
  3. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,452
    Roedy Green
    Aug 10, 2003
  4. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    482
    Harry
    Sep 23, 2003
  5. nbigaouette

    Z-Ordering (Morton ordering) question

    nbigaouette, Nov 5, 2009, in forum: C Programming
    Replies:
    2
    Views:
    2,218
Loading...

Share This Page