Help with sorting values in a TreeMap

W

Wendy S

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?
 
C

Collin VanDyck

Wendy S said:
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.
 
W

Wendy S

Collin VanDyck said:
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!
 
H

hiwa

Wendy S said:
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.
 
J

Jim McMaster

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.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top