I have created a map object that stores the key and the value, but it has to sort the values by decesinding order,I was thinking of making a treemap to sort it in accesinding order.Can some one please help me to solve this with a small piece of code.I dont know much on the subject of sorting and searching so any help would be greatly appericated.
You will have to give some more information about what you are trying to accomplish. Usually you put something in a map to be able to retrieve it based on the key. You can also use the values() method of the Map to retrieve the values and sort them how you need them.
Joined: May 01, 2004
Thanks for quick reply bart zagers, actually I am trying to sort the map on key. please help me with a small piece of code.
I have created a map object that stores the key and the value, but it has to sort the values by descending order
How frequently do you need this sorted representation of your data?
If you need a sorted representation very infrequently, a TreeMap might be overkill ... because add/remove methods for TreeMap run in O(lgn), which is not as good as the O(1) run time for HashMap's add/remove methods.
On the other hand, if you need to operate on the elements in a sorted fashion frequently, then you'll want to avoid paying O(n lg n) to obtain a sorted representation ... so maintaining a sorted structure could be advantageous and TreeMap might be a good candidate.