This question about Collections
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, December 8, 2006 02:32 AM

I have HashMap data


If i print that HashMap Data that is not the order as i insert why


if any please give the solution.

Re: This question about Collections

Posted By:   rahul123  
Posted On:   Wednesday, December 18, 2013 04:25 AM

in hashmap data is stored in key-value pair .
insertion order is depend on hashcode based on key . so in hashmap insertion order is not preserve.

Re: This question about Collections

Posted By:   Robert_Lybarger  
Posted On:   Friday, December 8, 2006 08:53 AM

Did you bother to read the API docs first? Probably not. Here, I'll even help you not have to try too hard: HashMap API docs. To quote from the first paragraph:


This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Re: This question about Collections

Posted By:   WarnerJan_Veldhuis  
Posted On:   Friday, December 8, 2006 02:54 AM

A HashMap does not retain order. It uses a key/value pair to store data instead of indices. Use a List for that, or like the other answer, a LinkedHashMap.

Re: This question about Collections

Posted By:   Anjan_Bacchu  
Posted On:   Friday, December 8, 2006 02:39 AM

Use LinkedHashMap(available from Java 1.4 onwards).



BR,

~A
About | Sitemap | Contact