Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in HashMap

 
Arjun Karthick
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15364
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21199
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic