Maps are not intended to have an insertion order. There is however a hybrid implementation called something like LinkedHashMap which combines a map with a linked list and the linked list records the insertion order. That might help. Go through the Java Tutorials and you can read about the Map implementations.
Originally posted by suresh midde: I have tried with the following code,
LinkedHashMap lmap = new LinkedHashMap(map);
I am retrieving the same order as map is returning me.
What is the map you are passing to the constructor ? I know the Javadoc says that this constructor creates an insertion-ordered LinkedHashMap, but I don't see how it could know the order if the map you are passing to it is a HashMap. It probably just iterates through the map that you pass in and adds the entries to itself and then takes that as the insertion order, which is why you get the same order as iterating through the HashMap. Try your original code but with a LinkedHashMap and see if that gives you the correct order.
[ September 02, 2008: Message edited by: Joanne Neal ]
Joined: Aug 22, 2008
Hi I can do that using
Map map = new LinkedHashMap(); map.put("one","a"); map.put("two","b"); map.put("three","c");
But i dont want to change the existing map;
Map map = new HashMap(); map.put("one","a"); map.put("two","b"); map.put("three","c");