Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Keeping Order of Objects using a HashMap (j2se 1.3) (http://www.velocityreviews.com/forums/t132834-keeping-order-of-objects-using-a-hashmap-j2se-1-3-a.html)

charles 04-23-2004 05:19 PM

Keeping Order of Objects using a HashMap (j2se 1.3)
 
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

Steve W. Jackson 04-23-2004 07:10 PM

Re: Keeping Order of Objects using a HashMap (j2se 1.3)
 
In article <408994a6.0404230919.785b3b6c@posting.google.com >,
charles_boyd11@yahoo.com (charles) wrote:

>: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


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 =
--
Steve W. Jackson
Montgomery, Alabama

Dale King 04-28-2004 08:39 PM

Re: Keeping Order of Objects using a HashMap (j2se 1.3)
 
"charles" <charles_boyd11@yahoo.com> wrote in message
news:408994a6.0404230919.785b3b6c@posting.google.c om...
> 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/co...ONS_3_0/org/ap
ache/commons/collections/map/LinkedMap.html


--
Dale King
Blog: http://daleking.homedns.org/Blog




mukesh yadav 12-14-2012 03:52 PM

Try to use LinkedHashmap instead of hashmap


All times are GMT. The time now is 09:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.