• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding sorting of TreeMap

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a LinkedHashMap.
 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80936
520
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic