File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Retrieving a Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Retrieving a Map" Watch "Retrieving a Map" New topic
Author

Retrieving a Map

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi ,
I have a HashMap with some values , And I want to retrieve that values .
--------------
1 --- Yes
2 --- No
3 --- Can't Say
--------------

what I am doing is :


But the problem with this is , I am getting values in reverse order , Like this :
-------
Can't Say
No
Yes
-------

And I need the values in correct order ( the order , I am adding values )

please help me , what should I do for this ... The code should be efficient also ...

thanks a lot .
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
Hi Rathi,
HashMap doesn't guarantee the order of the entries in the map will remain the same over time as the order when the entries were added to the map. You should use LinkedHashMap instead.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
No but I am getting a particular order that is opposite order ...

Please relpy any body ...

thanks a lot .
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
Rathi,
Let me explain it to you more detail. When I said that the order of the entries in a HashMap can't be guaranteed will remain the same over the time, it means that whenever an entry is added or removed from a HashMap, the order of the entries inside it may change. In your case that the order are the opposite, it's just a coincident.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Although I it is tough for me to beleive that it is just am coincident , because coincident doesn't happen everytime . You can try ...
I think their is some confusion because of Set ... but not sure ...

OK but I agreed , So can you tell me , what should I do , what my requirement is , I have to retrieve the values of Map one by one ( only values ) in a order , I have entered . I am using j2se 1.3 so can't use LinkedHashMap .

thanks a lot .
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Exactly, HashMap is an unordered collection. You can either use LinkedHashMap (new in JDK 1.4) which maintains the elements in the same order in which they were added or TreeMap which will order the values using the natural ordering of their keys. Since your keys are Integers, this will work without any more work on your part since you want them in numeric order.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
My problem is solved using TreeMap . But the question is , Is this efficint & only way to retrieve the values ( only values one by one ) of a Map ?



thanks . please reply .
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
If only the values you are concerning of, why don't you just use List instead of Map? But if you still want to use Map, it has values() method that return a Collection.

ps.
Rathi, if you have a problem with Java (or other languages), please try to get the solution first by reading the API documentation before asking for help form other people. This also true for other languages.
[ February 15, 2005: Message edited by: Yosi Hendarsjah ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving a Map