Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Use hashmap in expression for std:foreach loop

48 views
Skip to first unread message

Jonathan Nagulesan

unread,
Jan 9, 2024, 8:40:32 PM1/9/24
to TeaVM
Dear TeaVM team,

Do you know if a hashmap can be used in a std:foreach loop?.
<std:foreach var="optName2" index="r" in="Arrays.asList(mpStrMultiChoice.get(p))">
</std:foreach>


The p is the index of an outer std:for each loop. The p is used as a key in the hashmap to extract a string array that is converted to a list via "Arrays.toList".

I use the following imports in the html template:
<?import java.util.Arrays?>
<?import java.util.Map?>
<?import java.util.HashMap?>


mpStrMultiChoice is a hashmap defined in the class file:
public Map<Integer,String[]> mpStrMultiChoice = new HashMap<>();

I get the error when compiling:
 Method get(java.lang.Object) is not applicable to given arguments

Thanks.

Jonathan Nagulesan

unread,
Jan 9, 2024, 9:58:51 PM1/9/24
to TeaVM
Never mind have figured out what to do to get it to work.
<std:foreach var="optName2" index="r" in="Arrays.asList(mpStrMultiChoice.get((Integer)p))">
<option html:value="optName2"><html:text value="optName2"></option>
</std:foreach>


I have to type cast the index to type (Integer). 

Regards,
Jonathan

ScraM Team

unread,
Jan 9, 2024, 10:05:07 PM1/9/24
to TeaVM
I'm glad that worked!  Thanks for letting us know.

While it sounds like you got it working, there is a neat Minesweeper example with double-nested foreach loops here in case someone else wants a related example:
Reply all
Reply to author
Forward
0 new messages