wood burning stoves 2.0*
The moose likes Java in General and the fly likes retrieving Hashmap keys in order of entry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "retrieving Hashmap keys in order of entry" Watch "retrieving Hashmap keys in order of entry" New topic
Author

retrieving Hashmap keys in order of entry

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hello,
I am putting data into a HashMap, and as I put it in, the key's are already in the sort order that I want them to be. However, I understand that if I use map.keySet() I may not retrieve my keys in the same order that I put them into the map. Do you think it is too bulky of a solution to also store the key's into an array at the same time, and then get the array items in order ( zero to arraySize) to use as keys into the hashmap? Or I could put the keys into an ArrayList and retrieve an ieterator of keys in order that way.
Any advice appreciated,
Thanks.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
If using 1.4 you can use LinkedHashMap. This will do exactly what you want.
If not, storing the keys in insertion order in a List is not pareticularly wasteful as you are aonly storing a reference to the objects not the objects themselves.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Don,
Thanks so much! That is exactly what I needed, but I wasn't aware that there was such an object as LinkedHashMap. I just looked it up and read more about it on the Sun website. This is very helpful to me, thank you very much,
Kim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving Hashmap keys in order of entry
 
Similar Threads
retrieve HashMap - key, value
Display of hashmap values using jstl
Hashtable / HashMap Problem.
Question about Sorting with Hashtable
HashMap