TreeMap - getting values out in the order they went in?

Discussion in 'Java' started by news.amnet.net.au, Apr 22, 2004.

  1. Hi

    I am using a TreeMap to store keys with each key referring to an Arraylist
    of values.

    However perhaps a TreeMap is not quite the right thing to use as it is very
    important that when I use an iterator to read out the keys, I get the values
    out exactly in the same order as they went in. This is not happening.

    I have a bean, with id "mb" which populations the TreeMap with keys, and for
    each key, an ArrayList with values.

    In my jsp I access the bean's method to population the TreeMap:

    TreeMap relationships = mb.getAllRelationships(leftwardfile);

    However, when I iterate over the TreeMap, The values do not come our in the
    order they went in. Here is my iteration:

    Set keySet = relationships.keySet();
    Iterator it = keySet.iterator();
    while (it.hasNext()) {

    [print out ArrayList values for each key]

    ....it looks like the values come out alphabetically in regards to the keys,
    rather than first key in -> first key out.

    Is there any other way I can store ArrayLists mapped to keys but where I can
    get first key in -> first key out?

    Perhaps I need another type of Collection, a List itself perhaps? But a List
    does not cater for key -> value relationship and I need the keys to check
    that things aren't repeated when the Collection is built. But when I want to
    get the values out by iterating over them it needs to be first in -> first
    out. Can anyone help with this?

    Any help will be greatly appreciated.

    Thanks

    Hugo
     
    news.amnet.net.au, Apr 22, 2004
    #1
    1. Advertising

  2. "news.amnet.net.au" <> wrote in message
    news:newscache$sq9kwh$zkc$...
    > Hi
    >
    > I am using a TreeMap to store keys with each key referring to an Arraylist
    > of values.
    >
    > However perhaps a TreeMap is not quite the right thing to use as it is

    very
    > important that when I use an iterator to read out the keys, I get the

    values
    > out exactly in the same order as they went in. This is not happening.
    >
    > I have a bean, with id "mb" which populations the TreeMap with keys, and

    for
    > each key, an ArrayList with values.
    >
    > In my jsp I access the bean's method to population the TreeMap:
    >
    > TreeMap relationships = mb.getAllRelationships(leftwardfile);
    >
    > However, when I iterate over the TreeMap, The values do not come our in

    the
    > order they went in. Here is my iteration:
    >
    > Set keySet = relationships.keySet();
    > Iterator it = keySet.iterator();
    > while (it.hasNext()) {
    >
    > [print out ArrayList values for each key]
    >
    > ...it looks like the values come out alphabetically in regards to the

    keys,
    > rather than first key in -> first key out.
    >
    > Is there any other way I can store ArrayLists mapped to keys but where I

    can
    > get first key in -> first key out?
    >
    > Perhaps I need another type of Collection, a List itself perhaps? But a

    List
    > does not cater for key -> value relationship and I need the keys to check
    > that things aren't repeated when the Collection is built. But when I want

    to
    > get the values out by iterating over them it needs to be first in -> first
    > out. Can anyone help with this?
    >
    > Any help will be greatly appreciated.
    >
    > Thanks
    >
    > Hugo
    >
    >


    Use a LinkedHashMap.

    Silvio Bierman
     
    Silvio Bierman, Apr 22, 2004
    #2
    1. Advertising

  3. news.amnet.net.au

    Roedy Green Guest

    On Thu, 22 Apr 2004 15:20:52 +0800, "news.amnet.net.au"
    <> wrote or quoted :

    >...it looks like the values come out alphabetically in regards to the keys,
    >rather than first key in -> first key out.
    >
    >Is there any other way I can store ArrayLists mapped to keys but where I can
    >get first key in -> first key out?


    1. You can have several collections indexing the same set of objects.
    ..e.g. you can look up by index number with an ArrayList AND lookup by
    exact key with a HashMap AND lookup in sorted order by TreeMap.

    2. If you want things sorted, the easiest way is to extract as
    ArrayList and sort every time you need the list. see
    http://mindprod.com/jgloss/sort.html. The alternative is to use a
    TreeMap that keeps things sorted. Try both ways to understand when
    each works better.

    3. See http://mindprod.com/products.html#SORTEDARRAYLIST

    4. ArrayList is the easiest to lookup in entry order. If you wanted
    to be silly you could generate keys by adding one each time to create
    Integer objects and using a HashMap or TreeMap lookup. Don't do it,
    unless you are just experimenting.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 22, 2004
    #3
    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. Wendy S
    Replies:
    4
    Views:
    14,263
    Jim McMaster
    Feb 19, 2004
  2. news.amnet.net.au

    Empty values in TreeMap - cannnot be removed?

    news.amnet.net.au, Apr 19, 2004, in forum: Java
    Replies:
    3
    Views:
    943
    Roedy Green
    Apr 19, 2004
  3. Replies:
    3
    Views:
    545
    John C. Bollinger
    Mar 8, 2006
  4. Asfand Yar Qazi
    Replies:
    4
    Views:
    419
    Asfand Yar Qazi
    Nov 12, 2004
  5. jonny
    Replies:
    1
    Views:
    489
    Michael Nemtsev
    Oct 6, 2007
Loading...

Share This Page