Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TreeMap - getting values out in the order they went in?

Reply
Thread Tools

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

 
 
news.amnet.net.au
Guest
Posts: n/a
 
      04-22-2004
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


 
Reply With Quote
 
 
 
 
Silvio Bierman
Guest
Posts: n/a
 
      04-22-2004

"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


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-22-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeMap printout order chunji08@gmail.com Java 3 03-08-2006 05:14 AM
My S2's CCD went screwy, just like they said it would Brian Zinchuk Digital Photography 6 10-19-2005 02:59 PM
Empty values in TreeMap - cannnot be removed? news.amnet.net.au Java 3 04-19-2004 08:57 PM
Help with sorting values in a TreeMap Wendy S Java 4 02-19-2004 07:17 AM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57