• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting values in a sequence from hashmap  RSS feed

 
Dheeraj Gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we get the values from a hashmap in the same sequence in which we have entered the values in hashmap? We cannot use key for storing the sequence info.
 
Abhik Sarkar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dheeraj,

I have come across the same requirement at times, and as far as I know it is not possible.

When I need this functionality very badly, I maint two collections. One in a Map and another in a list. When I need sequential access, I use the list, and when I need high speed access by key, I use the map.

Of course, one has to make sure that both collections are always sycn'ed.

I don't know whether this is the best approach, but it works for me.

HTH,
Abhik.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

In JDK 1.4 and later, there's a new class named LinkedHashMap which has exactly the behavior you're after; just sue it instead of HashMap.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!