• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Retrieving insertion order of a map

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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: 69831
278
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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,

 
You don't like waffles? Well, do you like this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic