Re: The new map syntax
"Marc Palmer" <[email protected]> Fri, 18 Nov 2005 10:58:55 +0000
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
-----Original Message----- From: "Nikhil G. Daddikar" <[email protected]> To: [email protected] Date: Fri, 18 Nov 2005 10:52:24 +0530 Subject: [WebMacro-user] The new map syntax > I like the new #set $map = { k1: v1, k2:v2 } syntax. The only problem > is > that it does not use the "List Ordered" Map i.e. when I want to say > render a select list from it, it shows things not in the order in > which > the map was defined. Is there a way where I can specify the > implementation class to use? I use JDK 1.5 and would like to specify > the LinkedHashMap. No, I don't believe this is possible. I'd say it's generally better not rely on the order of definition of map elements. If you need ordering you can simply create 2 lists. It's not that pretty but... Alternatively, and usually this makes sense for "human" use, you would populate lists etc based on a sort order rather than listed order - so you can just write a trivial helper to return a sorted list of the keys: public class SortTool { public static final Object sort(Object list) { List l = ListUtil.toList(list); Collections.sort(l); return l; } } ...and use it like this: #set $sortedKeys = $Sorter.sort($myMap.keySet().iterator()) ...of course you can make the usage of this tool a LOT nicer by adding some more intelligence / overidden methods: public class SortTool { public static final Object sort(Object list) { List l = ListUtil.toList(list); Collections.sort(l); return l; } public static final Object sortKeys(Map map) { return sort( map.keySet().iterator()); } } ...and use it like this: #set $sortedKeys = $Sorter.sortKeys($myMap) Cheers ------------------------------------------------------- This SF.Net email is sponsored by the JBoss Inc. Get Certified Today Register for a JBoss Training Course. Free Certification Exam for All Training Attendees Through End of 2005. For more info visit: http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click