Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting in TreeMap

Status
Not open for further replies.

MENON

Programmer
Jul 23, 2001
28
US
All,

I have a TreeMap which is sorted by key. However I have requirement to sort by value instead. Can I do this using a comparator? Can someone give me code example? I am relatively new to Java.

Thanks,
 
Well, you can't sort the keys AND the values in the same data structure. You could extract the values into a TreeSet. They would then be sorted and you could iterate through them:

TreeMap map = new TreeMap();
...
TreeSet set = new TreeSet(map.values());

Iterator i = set.iterator();

// Now iterate through the sorted elements

Of course, this assumes that the values in your TreeMap implement the Comparable interface...

Regards,

Charles
 
Hi Charles,

But then I lose the Key-value interaction. How can I get the value in TreeSet associated with Key from original TreeMap?

Any other suggestions?

Thanks,
Rakesh
 
At this point, you will probably need to actually tell me what problem it is that you are trying to solve or how you are trying to use the data. Otherwise, I am just shooting in the dark.

Please elaborate on what data is being stored and what you are trying to do with it.
 
Hi Charles,

Here is a sample data... that I have inserted in TreeMap
ABC America
AEN Netharlands Ando
AND Andaman
BEC Barbados
NED Netherlands

EJB returns resultset of above data that has been loaded into a TreeMap. Just to give you some history...
We tried using simple Map and added sort to SQL but that doesn't work. So we decided to use TreeMap. However TreeMap sorts by Key. So when I transform it to HTML this is how it looks
<OPTION value=&quot;ABC&quot;>America</OPTION>
<OPTION value=&quot;AEN&quot;>Netherlands Ando</OPTION>
<OPTION value=&quot;AND&quot;>Andaman</OPTION>
<OPTION value=&quot;BEC&quot;>Barbados</OPTION>
<OPTION value=&quot;NED&quot;>Netherlands</OPTION>
and hence the order doesn't work well, bcos I want to sort by description and not code.

i.e. I want to sort by value in Map.

I hope I have explained well. Please let me know if you have further questions.

Thanks,
Rakesh
 
Ok, here is some code that essentially reverses the TreeMap, i.e. it swaps the values with the keys so that the new Treemap is sorted by the values of the previous tree. Of course, this means that the keys become the values and vice versa but this shouldn't matter to you for output purposes. You can then use the iterator to populate your HTML code dynamically via a servlet of jsp or whatever.

Since a TreeMap is guaranteed to have log(N) lookup and insertion, this is relatively inexpensive, i.e. O(nlogn)

public static TreeMap keysToValues(TreeMap t)
{
// Create map to be returned
TreeMap newmap = new TreeMap();

// Get the keys from the incoming TreeMap
Set keys = t.keySet();
Iterator i = keys.iterator();
String key = null;

//Extract key/value pairs and insert in new Tree
while (i.hasNext())
{
key = (String)i.next();
newmap.put(t.get(key), key);
}
return newmap;
}

Hope this helps...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top