Keeping Order of Objects using a HashMap (j2se 1.3)

Discussion in 'Java' started by charles, Apr 23, 2004.

  1. charles

    charles Guest

    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
    charles, Apr 23, 2004
    #1
    1. Advertising

  2. In article <>,
    (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
    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Apr 23, 2004
    #2
    1. Advertising

  3. charles

    Dale King Guest

    "charles" <> wrote in message
    news:...
    > 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


    --
    Dale King
    Blog: http://daleking.homedns.org/Blog
    Dale King, Apr 28, 2004
    #3
  4. charles

    mukesh yadav

    Joined:
    Dec 14, 2012
    Messages:
    1
    Try to use LinkedHashmap instead of hashmap
    mukesh yadav, Dec 14, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pembed2003
    Replies:
    4
    Views:
    9,156
    pembed2003
    Apr 4, 2004
  2. Vince Darley
    Replies:
    4
    Views:
    4,416
    emilchacko
    Mar 2, 2010
  3. Martijn Mulder

    J2SE 1.4.2 or J2SE 5.0?

    Martijn Mulder, Jun 27, 2005, in forum: Java
    Replies:
    15
    Views:
    1,836
    Tor Iver Wilhelmsen
    Jun 28, 2005
  4. Replies:
    0
    Views:
    516
  5. Rakesh
    Replies:
    10
    Views:
    12,169
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page