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

N

news.amnet.net.au

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
 
S

Silvio Bierman

news.amnet.net.au said:
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
 
R

Roedy Green

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

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top