• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of Map

 
thippareddy mallikarjuna reddy
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pals

I am using HashMap.iam adding some values in to HashMap. for example. i added 200 results in to HashMap .first 1000 results of HashMap belongs to one part and another 100 results of HashMap belongs to another part.

My problem is :

The Hashmap does not maintain order which i entered into HAshMap.
the results are combined.
now i have to maintain order....

can you advice me which collection calss will work for this type of probelm..

...Thanks advance

Thanks
Malliakrjuna
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add your values to a HashMap, as you know, you have to specify a key and a value, and the HashMap takes the hashCode of the key to decide where to put the value. So unless you have keys which return different hashCodes from one another, you are going to find the values mixed up together.

I can't offhand think of a Collection type which will separate values like that; have a look in the Collections part of the Java tutorial and see whether that helps. You could subdivide a Collection by index, but that fixes its size and vitiates the benefit of Collection classes altoghether.

Does anybody else know of a collection which can be subdivided like that?
 
thippareddy mallikarjuna reddy
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi CampBell,

Thanks reply.

I added values into HAshMAp based on Key.first 100 keys belongs to one part and second 200 keys belongs to another part. now i got output with mixed. so now i need to get order which i entered in to Hashmap

Thanks
Mallikarjuna
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashMap
 
thippareddy mallikarjuna reddy
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff Albertson,

Thank you.

cheers
Mallikarjuna
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic