• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieving insertion order of a map

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
suresh midde
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic