Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving Hashmap keys in order of entry

 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic