• 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

Doubt in HashMap

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same question twice.
 
Arjun Karthick
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use LinkedHashSet instead of HashSet. The linked Hashset lets you iterate through the elemenets in the order in which they are inserted.
 
Arjun Karthick
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remko already gave you (almost) the answer. Use LinkedHashMap instead of HashMap. LinkedHashMap preserves the order of the elements.
 
Arjun Karthick
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Arjun Karthick
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic