Re: Accessing a key in a java map.
Sergiu Dumitriu <[email protected]>
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Organization | XWiki |
| Message-ID | <[email protected]> |
On 06/01/2012 01:20 PM, Lukas, Ray wrote:
> I have created a java.util.HashMap<String, TressSet<MyJavaObject>> and have placed that into the $values map which I then pass into Velocity. The name of a group of things mapped to a sorted set of those things.
> I have no problem iterating through the map but I cannot seem to find a way to access the key (the String) of the map.entry and overlay that into my form. How is this done. I want to print out the name of the group and then the group. Common thing to do I would think..
>
> #foreach($LabTestTypeEntrySet in $values.TestTypeGroupedBySpecimenName.keySet())
> $LabTestTypeEntrySet.??? - what goes here ---??
> #foreach($LabTestType in $values.TestTypeGroupedBySpecimenName.get($LabTestTypeEntrySet) )
> $LabTestType.labTestTypeCode
> #end
> #end
>
> I am kind of new to Velocity and have been hunting for the solution.. can someone help me out real quick.
> Thanks guys..
>
Behind any velocity variable there's a real Java object. So, given that,
as you said, $values is a HashMap<String, TreeSet>, then what would you
do to get the key in Java?
There are some things that don't seem to be right in your code...
The name of $LabTestTypeEntrySet seems wrong, since the variable that
you're iterating with will not hold a set, but only one value at a time,
so it should be called $LabTestTypeEntry.
$values.TestTypeGroupedBySpecimenName translates into HashMap<String,
TreeSet>.get('TestTypeGroupedBySpecimenName'), which returns a value
from the map, which should have the type TreeSet<MyJavaObject>. There's
no keySet() method for a TreeSet, so that whole #foreach shouldn't have
any values to iterate on...
Perhaps you placed this whole map as an entry into the the $values
object, under the 'TestTypeGroupedBySpecimenName' key? In that case,
this code should work:
#foreach($LabTestTypeEntry in
$values.TestTypeGroupedBySpecimenName.entrySet())
## Notice that I used entrySet(), not keySet()
## $LabTestTypeEntry.key is the key, of type String
## $LabTestTypeEntry.value is the value, of type TressSet<MyJavaObject>
#foreach($LabTestType in $LabTestTypeEntry.value)
## $LabTestType is a MyJavaObject from the TreeSet
$LabTestType.labTestTypeCode
## I guess that your MyJavaObject has a getLabTestTypeCode() method
#end
#end
--
Sergiu Dumitriu
http://purl.org/net/sergiu/