This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes order of  Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "order of  Map" Watch "order of  Map" New topic

order of Map

thippareddy mallikarjuna reddy

Joined: Mar 08, 2006
Posts: 19
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

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43995
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

Joined: Mar 08, 2006
Posts: 19
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

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
thippareddy mallikarjuna reddy

Joined: Mar 08, 2006
Posts: 19
Hi Jeff Albertson,

Thank you.

Have you checked out Aspose?
subject: order of Map
It's not a secret anymore!