Forums Register Login

Getting values in a sequence from hashmap

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2421 times.
Similar Threads
Retrieve Value with out key in hashmap
Collection issue
hashmap key values
hashmap with null values and keys
Data Caching Best Practices ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:37:22.