Help with sorting values in a TreeMap

Discussion in 'Java' started by Wendy S, Feb 13, 2004.

  1. Wendy S

    Wendy S Guest

    I have a TreeMap that stores a bunch of beans. The 'key' to the Map is a
    String, the account number, in the form 32_X_MADN0005, and the 'value' in
    the map is an AccountView object whose 'getKey()' method returns the
    aforementioned String.

    I use TreeMap so that they sort nicely by the account number.

    But now the users want to be able to choose to sort the accounts by the cost
    center, which is the 'MADN0005' part in the example above. So I set about
    writing a Comparator. Easy enough... except that TreeMap sorts on the
    *keys*, and those are Strings. :(

    I need the Map, because I need to look-up values with the key in other parts
    of the app.

    Do I construct an List with the correct Comparator, and use that to display
    the items? It shouldn't take up too much extra memory, right? Since both
    the List and the Map will just have references to the same AccountView
    objects?

    Or do I stop using a String for the key to the Map, and instead use
    something else and write Comparators for *that* class?

    Can anyone offer some advice?

    --
    Wendy in Chandler, AZ
     
    Wendy S, Feb 13, 2004
    #1
    1. Advertising

  2. "Wendy S" <> wrote in message
    news:c0j8n7$g6e$...
    > I have a TreeMap that stores a bunch of beans. The 'key' to the Map is a
    > String, the account number, in the form 32_X_MADN0005, and the 'value' in
    > the map is an AccountView object whose 'getKey()' method returns the
    > aforementioned String.
    >
    > I use TreeMap so that they sort nicely by the account number.
    >
    > But now the users want to be able to choose to sort the accounts by the

    cost
    > center, which is the 'MADN0005' part in the example above. So I set about
    > writing a Comparator. Easy enough... except that TreeMap sorts on the
    > *keys*, and those are Strings. :(
    >


    When you construct a TreeMap with a Comparator , the TreeMap will sort the
    entries in the Map according to the Comparator. Your Comparator would then
    be able to extract the 32_X_ (or similar) from the objects it is comparing.
    The objects that it would be comparing _are_ the keys that you described, so
    extracting the cost center from the keys should be trivial inside of your
    comparator.

    From the javadocs:

    Constructs a new, empty map, sorted according to the given comparator. All
    keys inserted into the map must be mutually comparable by the given
    comparator: comparator.compare(k1, k2) must not throw a ClassCastException
    for any keys k1 and k2 in the map. If the user attempts to put a key into
    the map that violates this constraint, the put(Object key, Object value)
    call will throw a ClassCastException.
     
    Collin VanDyck, Feb 13, 2004
    #2
    1. Advertising

  3. Wendy S

    Wendy S Guest

    "Collin VanDyck" <> wrote:
    > When you construct a TreeMap with a Comparator , the TreeMap will sort the
    > entries in the Map according to the Comparator. Your Comparator would

    then
    > be able to extract the 32_X_ (or similar) from the objects it is

    comparing.
    > The objects that it would be comparing _are_ the keys that you described,

    so
    > extracting the cost center from the keys should be trivial inside of your
    > comparator.


    See, I knew I was just confused. When I wrote the Comparator, I wrote it to
    compare the AccountView objects, and then couldn't figure out how to get
    that to work with the Map. Writing a Comparator to compare the Strings
    makes perfect sense. :)

    Thanks!
    --
    Wendy Smoak
     
    Wendy S, Feb 13, 2004
    #3
  4. Wendy S

    hiwa Guest

    "Wendy S" <> wrote in message news:<c0j8n7$g6e$>...
    > I have a TreeMap that stores a bunch of beans. The 'key' to the Map is a
    > String, the account number, in the form 32_X_MADN0005, and the 'value' in
    > the map is an AccountView object whose 'getKey()' method returns the
    > aforementioned String.
    >
    > I use TreeMap so that they sort nicely by the account number.
    >
    > But now the users want to be able to choose to sort the accounts by the cost
    > center, which is the 'MADN0005' part in the example above. So I set about
    > writing a Comparator. Easy enough... except that TreeMap sorts on the
    > *keys*, and those are Strings. :(
    >
    > I need the Map, because I need to look-up values with the key in other parts
    > of the app.
    >
    > Do I construct an List with the correct Comparator, and use that to display
    > the items? It shouldn't take up too much extra memory, right? Since both
    > the List and the Map will just have references to the same AccountView
    > objects?
    >
    > Or do I stop using a String for the key to the Map, and instead use
    > something else and write Comparators for *that* class?
    >
    > Can anyone offer some advice?


    I believe if you give your Comparator to your (another working
    temporary?) TreeMap constructor, or your TreeMap#comparator() method
    returns it, everything should be fine.
     
    hiwa, Feb 14, 2004
    #4
  5. Wendy S

    Jim McMaster Guest

    Be sure no two items have the same cost center. The second occurrence of
    the same key deletes the previous value. I found out the hard way, when I
    tried using a TreeMap to sort items by price.

    You might want to use Collections.sort(), with the appropriate comparator.

    --
    --
    Jim McMaster
    mailto:
    "Wendy S" <> wrote in message
    news:c0j8n7$g6e$...
    > I have a TreeMap that stores a bunch of beans. The 'key' to the Map is a
    > String, the account number, in the form 32_X_MADN0005, and the 'value' in
    > the map is an AccountView object whose 'getKey()' method returns the
    > aforementioned String.
    >
    > I use TreeMap so that they sort nicely by the account number.
    >
    > But now the users want to be able to choose to sort the accounts by the

    cost
    > center, which is the 'MADN0005' part in the example above. So I set about
    > writing a Comparator. Easy enough... except that TreeMap sorts on the
    > *keys*, and those are Strings. :(
    >
    > I need the Map, because I need to look-up values with the key in other

    parts
    > of the app.
    >
    > Do I construct an List with the correct Comparator, and use that to

    display
    > the items? It shouldn't take up too much extra memory, right? Since both
    > the List and the Map will just have references to the same AccountView
    > objects?
    >
    > Or do I stop using a String for the key to the Map, and instead use
    > something else and write Comparators for *that* class?
    >
    > Can anyone offer some advice?
    >
    > --
    > Wendy in Chandler, AZ
    >
    >
     
    Jim McMaster, Feb 19, 2004
    #5
    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. Ahmed Moustafa

    HashMap vs TreeMap

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

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    475
    Harry
    Sep 23, 2003
  3. news.amnet.net.au

    Empty values in TreeMap - cannnot be removed?

    news.amnet.net.au, Apr 19, 2004, in forum: Java
    Replies:
    3
    Views:
    926
    Roedy Green
    Apr 19, 2004
  4. news.amnet.net.au
    Replies:
    2
    Views:
    9,247
    Roedy Green
    Apr 22, 2004
  5. Luc The Perverse

    Help With TreeMap Warning (Noob?)

    Luc The Perverse, Dec 28, 2006, in forum: Java
    Replies:
    3
    Views:
    1,578
    Luc The Perverse
    Dec 28, 2006
Loading...

Share This Page