You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Robert Koberg <ro...@koberg.com> on 2005/04/05 14:07:12 UTC

SortedMap/TreeMap in a tool does not sort

Hi,

 From what I am seeing, it seems to be the case that velocity 
tools/templates do not work with SortedMap/TreeMap created in the tool. 
That is, they do not come to the template in the order they were placed 
in the map. Is that to be expected?

<my-case>I have a tool(s) that creates 'static final' question (and 
answer, if multichoice) objects. These question objects are added to a 
SortedMap. If the question object has multiple choice answers, those 
answers are also added to their own SortedMap. The answers come back in 
the put order, but the questions do not. (before anyone asks why I am 
creating static final question objects in a tool (quizes), the classes 
are generated by XSL at build time before compile)</my-case>

best,
-Rob

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: SortedMap/TreeMap in a tool does not sort

Posted by Robert Koberg <ro...@koberg.com>.
Shinobu Kawai wrote:
> Hi Rob,
> 
> 
>>I had assumed 'natural order' meant the order I put it in (never used
>>SortedMap before...). The question's answers are correct because the
>>keys are like q1, q2, q3.
> 
> 
> Just for you.  ;)
>     http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashMap.html


hmmmm... it seems that has just appeared in my (1.4.2) api... :)

thanks,
-Rob

> 
> If you're using a version less than 1.4, you can use commons-collections
>     http://jakarta.apache.org/commons/collections/
> 
> Best regards,
> -- Shinobu
> 
> --
> Shinobu Kawai <sh...@gmail.com>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: SortedMap/TreeMap in a tool does not sort

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Rob,

> I had assumed 'natural order' meant the order I put it in (never used
> SortedMap before...). The question's answers are correct because the
> keys are like q1, q2, q3.

Just for you.  ;)
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashMap.html

If you're using a version less than 1.4, you can use commons-collections
    http://jakarta.apache.org/commons/collections/

Best regards,
-- Shinobu

--
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: SortedMap/TreeMap in a tool does not sort

Posted by Robert Koberg <ro...@koberg.com>.
Simon Christian wrote:
> Hi Robert,
> 
> Remember that SortedMap/TreeMap only guarantee "that the map will be in 
> ascending key order, sorted according to the natural order for the key's 
> class". This is as opposed to the put order.
> 
> Looks like you need to either supply a Comparator when creating the 
> TreeMap, or implement Comparable in your question classes.

Guess I need to learn java :)

I had assumed 'natural order' meant the order I put it in (never used 
SortedMap before...). The question's answers are correct because the 
keys are like q1, q2, q3.

Thanks and sorry for the bother,
-Rob


> 
> hth,
> 
> - simon
> 
> Robert Koberg wrote:
> 
>> Hi,
>>
>>  From what I am seeing, it seems to be the case that velocity 
>> tools/templates do not work with SortedMap/TreeMap created in the 
>> tool. That is, they do not come to the template in the order they were 
>> placed in the map. Is that to be expected?
>>
>> <my-case>I have a tool(s) that creates 'static final' question (and 
>> answer, if multichoice) objects. These question objects are added to a 
>> SortedMap. If the question object has multiple choice answers, those 
>> answers are also added to their own SortedMap. The answers come back 
>> in the put order, but the questions do not. (before anyone asks why I 
>> am creating static final question objects in a tool (quizes), the 
>> classes are generated by XSL at build time before compile)</my-case>
>>
>> best,
>> -Rob
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: SortedMap/TreeMap in a tool does not sort

Posted by Simon Christian <si...@stoutstick.com>.
Hi Robert,

Remember that SortedMap/TreeMap only guarantee "that the map will be in 
ascending key order, sorted according to the natural order for the key's 
class". This is as opposed to the put order.

Looks like you need to either supply a Comparator when creating the 
TreeMap, or implement Comparable in your question classes.

hth,

- simon

Robert Koberg wrote:
> Hi,
> 
>  From what I am seeing, it seems to be the case that velocity 
> tools/templates do not work with SortedMap/TreeMap created in the tool. 
> That is, they do not come to the template in the order they were placed 
> in the map. Is that to be expected?
> 
> <my-case>I have a tool(s) that creates 'static final' question (and 
> answer, if multichoice) objects. These question objects are added to a 
> SortedMap. If the question object has multiple choice answers, those 
> answers are also added to their own SortedMap. The answers come back in 
> the put order, but the questions do not. (before anyone asks why I am 
> creating static final question objects in a tool (quizes), the classes 
> are generated by XSL at build time before compile)</my-case>
> 
> best,
> -Rob
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org