aspose file tools*
The moose likes Beginning Java and the fly likes Retrieving insertion order of a map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Retrieving insertion order of a map" Watch "Retrieving insertion order of a map" New topic
Author

Retrieving insertion order of a map

suresh midde
Greenhorn

Joined: Aug 22, 2008
Posts: 25
Hi,

I have a map, that holds some keys and values.

I want to retrieve the insertion order of the keys and values in the map.




Map map = new HashMap();
map.put("one","a");
map.put("two","b");
map.put("three","c");

I have tried with the following code,

LinkedHashMap lmap = new LinkedHashMap(map);

I am retrieving the same order as map is returning me.

can anyone help me?

Thanks & Regards
Suresh Midde
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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 ]

Joanne
suresh midde
Greenhorn

Joined: Aug 22, 2008
Posts: 25
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");



Is there anyway to do this?

Regards
Suresh
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by suresh midde:

But i dont want to change the existing map;

Is there anyway to do this?


No. That's what the LinkedHashMap is for.
This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving insertion order of a map