Re: Help with sorting values in a TreeMap

From: Jim McMaster (jim.mcmaster_at_comcast.net)
Date: 02/19/04


Date: Thu, 19 Feb 2004 00:17:17 -0700

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: jim.mcmaster@comcast.net
"Wendy S" <wendywds@hotmail.com> wrote in message
news:c0j8n7$g6e$1@news.asu.edu...
> 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
>
>


Relevant Pages

  • Help with sorting values in a TreeMap
    ... 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. ...
    (comp.lang.java.programmer)
  • Re: Oracle Connection Pooling
    ... > public String oracleFormatUrl(String prefix, String host, String port, ... > proprietaryException { ... > * Map from a resource attribute to a proprietary attribute. ... > protected void processCreateRequests ...
    (comp.lang.java.databases)
  • Re: Two problems sending an E-mail for Outlook 2007 and VB 2005
    ... I'd suspect that you aren't getting a valid Account object, I've used SendUsingAccount with valid Account objects with no problems, although reading that object property only works right in a Send event. ... Dim WordContent As String ... Dim SendPersonName As String ...
    (microsoft.public.outlook.program_vba)
  • Re: Irregular review procedures for IACR sponsored conferences
    ... There are only three theorems. ... The second says that if you apply the map to an arbitrary but sufficiently ... long string, substrings of the image are uniformly distributed. ... the policy might look pretty dubious. ...
    (sci.crypt)
  • Re: Testing Platforms
    ... I think that most of us would just run a separate account on the main ... String "q" ... programming changes relating to data entry screens, report formatting, ... we are current on our support contract) has run hot and cold. ...
    (comp.databases.pick)