Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   TreeMap - getting values out in the order they went in? (http://www.velocityreviews.com/forums/t132792-treemap-getting-values-out-in-the-order-they-went-in.html)

news.amnet.net.au 04-22-2004 07:20 AM

TreeMap - getting values out in the order they went in?
 
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



Silvio Bierman 04-22-2004 07:55 AM

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

"news.amnet.net.au" <hugo@geoinformex.com> wrote in message
news:newscache$sq9kwh$zkc$1@phantom.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
>
>


Use a LinkedHashMap.

Silvio Bierman



Roedy Green 04-22-2004 08:12 AM

Re: TreeMap - getting values out in the order they went in?
 
On Thu, 22 Apr 2004 15:20:52 +0800, "news.amnet.net.au"
<hugo@geoinformex.com> 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.


All times are GMT. The time now is 08:53 AM.

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