| Author |
Regarding sorting of TreeMap
|
yekkala krishna
Ranch Hand
Joined: Mar 04, 2007
Posts: 105
|
|
Hi,
In my application elements are added to a TreeMap.so whenever we are retrieving elements are displaying in the sorted order of keys.But i want to retrieve the elements in the order in which they are inserted.May i know which will be the correct choice now to do this order?
Thanks in advance.
regards,
rama krishna.y
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Use a LinkedHashMap.
|
[My Blog]
All roads lead to JavaRanch
|
 |
yekkala krishna
Ranch Hand
Joined: Mar 04, 2007
Posts: 105
|
|
Hi Verre,
Thanks for your reply.
Elements are displaying in the inserted order if i use LinkedHashMap.
Can i do the same thing using TreeMap?
regards,
Rama Krishna
|
 |
amit punekar
Ranch Hand
Joined: May 14, 2004
Posts: 488
|
|
Hi,
But i want to retrieve the elements in the order in which they are inserted
You need to use LinkedHashMap to do this. If you use TreeMap then you are going to get the results in sorted manner based on the Key.
Regards,
Amit
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
A collections class will display its elements in one order (or no particular order). They are not designed to display two different orders. If you want sorted order as well as insertion order, try your tree Map and a separate List, and add the same element to both. Or insert all your elements into the List and later pass both the key and value for every pair to the Map.
|
 |
 |
|
|
subject: Regarding sorting of TreeMap
|
|
|