Hi All, My requirement is to take the values of the HashMap and store that in a Collection in the order in which it was stored in the HashMap. The HashMap.Values() method returns a colection of values in the hashmap. But Collection in nature doesnot assure the order of reterival. My doubt is how to assure the order of reterival in the order in which it was stored in the map? Can anyone help in this ?
Joined: Oct 13, 2005
Please don't post the same question twice.
Joined: Jan 04, 2007
Hello Campbell Ritchie , Sorry. But i did it purposefully as i was not sure if it was a beginner's question or intermediate question.
Can some one throw light on the problem i am facing?
Joined: Dec 28, 2006
Use LinkedHashSet instead of HashSet. The linked Hashset lets you iterate through the elemenets in the order in which they are inserted.
No. i have the values stored in the hashmap.i wanted to reterive them in the order in which it was stored in the hashmap. Currently i am using HashMap.Values() method. It returns a collection. But reteriveing elements from the collection does not assure the order of reterival.
Can you give the code snippet that does this functionality as i dont understand what you are saying or i might not have explained the problem space in the forum properly, so that you were not able to answer the question i raised.
My doubt is will this code produce output name1#name2#name3#name4 always. i assume the issue is not in using the HashMap or LinkedHashMap. it is when the the values are stored in the collection by the method Values. the order of reterival is not assured when it is reterived from the collection by it's nature.
Or do you mean that if LinkedHashMap is used the output will be name1#name2#name3#name4 always.
Hash table and linked list implementation of the Map interface, with predictable iteration order. 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, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)