wood burning stoves*
The moose likes Beginning Java and the fly likes Doubt in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in HashMap" Watch "Doubt in HashMap" New topic
Author

Doubt in HashMap

Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
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 ?

regards,
Karthick.J.G
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Please don't post the same question twice.
Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
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?
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Use LinkedHashSet instead of HashSet. The linked Hashset lets you iterate through the elemenets in the order in which they are inserted.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
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.

regards,
Karthick.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Remko already gave you (almost) the answer. Use LinkedHashMap instead of HashMap. LinkedHashMap preserves the order of the elements.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77

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.

regards,
Karthick
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Or do you mean that if LinkedHashMap is used the output will be name1#name2#name3#name4 always.


Reading the JavaDoc should *always* be done first !! This is from the first paragraph of the JavaDoc for the LinkedHashMap.


public class LinkedHashMap
extends HashMap

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.)


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
Thanks Everyone.
Got to learn not only java. but also how to post a topic in forum.
Sorry for posting this topic without doing the initial research in it.

regards,
Karthick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in HashMap