Dumping HashMAp

Discussion in 'Java' started by Sridhar, Jun 29, 2005.

  1. Sridhar

    Sridhar Guest

    Consider the following snippet to display the contemts of a HashMap in
    a sequential manner.

    //temp2 is a hashmap
    Set set2=temp2.entrySet();
    Iterator iHash2=set2.iterator();

    while(iHash2.hasNext()){
    Map.Entry me2= (Map.Entry) iHash2.next();
    System.out.println(me2.getKey());
    }

    I printed the order in which the elements were entered in the HashMap
    and later printed the hashMap using the above algorithm... both are
    different.... Any idea why??

    I'm just printing the keys here. During insertion the order followed
    for the key is
    r1,s3,s2,s1

    but when i print the hashmap the order is:
    s2,s3,r1,s1

    Since i need to use it sequentially later in the application, the order
    is very important. I cant sort the hashmap too as keys have no relation
    whatsoever.

    Any pointers??
    Sridhar, Jun 29, 2005
    #1
    1. Advertising

  2. Hi,

    Sridhar wrote:
    > I printed the order in which the elements were entered in the HashMap
    > and later printed the hashMap using the above algorithm... both are
    > different.... Any idea why??


    Because HashMap does not say anything about the order! (Are you sure,
    you understand what a HashMap does? If not, do not use it, especially
    not with own objects, because you may be surprised about different
    issues as well!)

    Perhaps, SortedMap / TreeMap can help you. Otherwise, a
    (Array-/Linked)List might be what you are looking for.

    Ciao,
    Ingo
    Ingo R. Homann, Jun 29, 2005
    #2
    1. Advertising

  3. Sridhar

    Ace Guest

    Try LinkedHashMap

    Ingo R. Homann wrote:
    > Hi,
    >
    > Sridhar wrote:
    > > I printed the order in which the elements were entered in the HashMap
    > > and later printed the hashMap using the above algorithm... both are
    > > different.... Any idea why??

    >
    > Because HashMap does not say anything about the order! (Are you sure,
    > you understand what a HashMap does? If not, do not use it, especially
    > not with own objects, because you may be surprised about different
    > issues as well!)
    >
    > Perhaps, SortedMap / TreeMap can help you. Otherwise, a
    > (Array-/Linked)List might be what you are looking for.
    >
    > Ciao,
    > Ingo
    Ace, Jun 29, 2005
    #3
  4. Sridhar

    Sridhar Guest

    I guess you are right... I made the blunder of assuming that the order
    of the elements will be maintained. Will surely try out the other
    alternatives. Thanx everyone
    Sridhar, Jun 30, 2005
    #4
  5. Sridhar

    Joan Guest

    "Sridhar" <> wrote in message
    news:...
    > Consider the following snippet to display the contemts of a HashMap in
    > a sequential manner.
    >
    > //temp2 is a hashmap
    > Set set2=temp2.entrySet();
    > Iterator iHash2=set2.iterator();
    >
    > while(iHash2.hasNext()){
    > Map.Entry me2= (Map.Entry) iHash2.next();
    > System.out.println(me2.getKey());
    > }
    >
    > I printed the order in which the elements were entered in the HashMap
    > and later printed the hashMap using the above algorithm... both are
    > different.... Any idea why??
    >
    > I'm just printing the keys here. During insertion the order followed
    > for the key is
    > r1,s3,s2,s1
    >
    > but when i print the hashmap the order is:
    > s2,s3,r1,s1
    >

    Use TreeMap instead.
    Joan, Jun 30, 2005
    #5
  6. Sridhar

    Sridhar Guest

    Used LinkedHashMAp... It did the trick
    Sridhar, Jul 1, 2005
    #6
    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. Sajan

    Dumping real signals in VCD

    Sajan, Sep 19, 2003, in forum: VHDL
    Replies:
    8
    Views:
    7,109
    vlsi.engg27
    Jul 21, 2007
  2. kwaj
    Replies:
    2
    Views:
    466
  3. kamal
    Replies:
    0
    Views:
    980
    kamal
    Aug 12, 2003
  4. Vince Darley
    Replies:
    4
    Views:
    4,388
    emilchacko
    Mar 2, 2010
  5. Rakesh
    Replies:
    10
    Views:
    12,147
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page