• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieving a Map

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No but I am getting a particular order that is opposite order ...

Please relpy any body ...

thanks a lot .
 
Yosi Hendarsjah
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic