Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding sorting of TreeMap

 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a LinkedHashMap.
 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50241
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic