Keeping Order of Objects using a HashMap (j2se 1.3)

C

charles

Hi

I am using a Hashmap to store objects and their keys. I am using
jdk1.3 (j2se1.3)

I know that when we store objects in a hashmap, the order in which the
objects are stored in the hashmap are not the same as they were added.

What my requirement is that, I need to maintain the order of the
objects in the HashMap in the order in which I had added the objects
to the hashmap.

but what i see is that, the order gets changed.

Is there any way in which I can maintain order of the ojects, as they
were added to the hashmap.

Any help on this would be very helpful..

The code snippet where I am facing this problem is as follows...

public void processLine(String lineString){

String num = lineString.substring(2,22);
num = num.trim();
String suffix = lineString.substring(22,42);
suffix = suffix.trim();

keyObj adminKey =
new KeyObj();
adminKey.setId(num);
adminKey.setFieldType(suffix);

if(num == null || "".equals(num) ||
suffix == null || "".equals(suffix)){
} else {

hmRequestCollection.put(num + " " + suffix,
adminKey);
}

}//End of processLine method

thanks in advance

Charles
 
S

Steve W. Jackson

:Hi
:
:I am using a Hashmap to store objects and their keys. I am using
:jdk1.3 (j2se1.3)
:
:I know that when we store objects in a hashmap, the order in which the
:eek:bjects are stored in the hashmap are not the same as they were added.
:
:What my requirement is that, I need to maintain the order of the
:eek:bjects in the HashMap in the order in which I had added the objects
:to the hashmap.
:
:but what i see is that, the order gets changed.
:
:Is there any way in which I can maintain order of the ojects, as they
:were added to the hashmap.
:
:Any help on this would be very helpful..
:
:The code snippet where I am facing this problem is as follows...
:
:public void processLine(String lineString){
:
:String num = lineString.substring(2,22);
:num = num.trim();
:String suffix = lineString.substring(22,42);
:suffix = suffix.trim();
:
:keyObj adminKey =
:new KeyObj();
:adminKey.setId(num);
:adminKey.setFieldType(suffix);
:
:if(num == null || "".equals(num) ||
:suffix == null || "".equals(suffix)){
:} else {
:
:hmRequestCollection.put(num + " " + suffix,
:adminKey);
:}
:
:}//End of processLine method
:
:thanks in advance
:
:Charles

Didn't you already ask this elsewhere? Seems familiar over the last
couple of days.

Anyway, I think the simplest solution might be to put your keys in two
places. Obviously, you'll have to put them in your HashMap, but you
could also add them to an ArrayList. Anything you add to an ArrayList
via its add method is appended, and therefore it's FIFO. So if you need
to go through items in the order added, you simply go through the
ArrayList in top-down order by index, retrieve each key and use it to
get the appropriate item from your map.

Better yet, as suggested elsewhere, find a way to move up to Java 1.4
and use the LinkedHashMap to track items in insertion order.

= Steve =
 
D

Dale King

charles said:
Hi

I am using a Hashmap to store objects and their keys. I am using
jdk1.3 (j2se1.3)

I know that when we store objects in a hashmap, the order in which the
objects are stored in the hashmap are not the same as they were added.

What my requirement is that, I need to maintain the order of the
objects in the HashMap in the order in which I had added the objects
to the hashmap.

In JDK1.4 it is trivial as they have added the LinkedHashMap. For 1.3 you
either have to use a combination of collections or use a third party library
such as the Jakarata Commons Collections API that has this class:

http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_0/org/ap
ache/commons/collections/map/LinkedMap.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top