when i get the output i get in different order then the order i inserted, any idea how to get teh output in the same order as the insertion. I also tried to use "LinkedHashMap" i get the same output. Thanks aziz
Well, a TreeMap is a SortedMap but you'd have to provide your own Comparator to determine the order. That can be a bit tricky and doesn't feel appropriate given your data.
The first thing that sprang to mind is to use a List of Map.Entry. However, that has a strong smell of hack about it and you would only be able to access the entries using the List methods, not the Map methods, i.e. no automatic get value by key look-up.
After a little more thought I think the best solution is to use the OrderedMap implementation (LinkedMap) from Jakarta Commons Collections.
Originally posted by Aziz Dhanani: I also tried to use "LinkedHashMap" i get the same output.
Are you sure you did that right, Aziz? My suggestion would be to use LinkedHashMap, because it guarantees to preserve insertion order when iterating through the map. It works for me and I wonder why it didn't seem to work for you.
I wouldn't use OrderedMap if a JDK class will do just as well.
- Peter [ September 08, 2004: Message edited by: Peter den Haan ]
Joined: Aug 02, 2004
Having read a little further - ahem - if you instantiate a LinkedHashMap thus:
you get the elements returned in access order rather than insertion order. I imagine it's unlikely you're actually doing that. How are you iterating over the Map to determine the order?