You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Pascal Jacob <za...@wanadoo.fr> on 2008/10/23 00:18:11 UTC

How to iterate over two (or more) collections simultaneously ?

Hi again,

Is it possible to iterate over two (or more) collections SIMULTANEOUSLY 
using the same indice ?

Here is a example:

    <parameterMap id="create-data-table-param" class="java.util.HashMap">
        <parameter property="names" javaType="string" /> <!-- really an 
array of string -->
        <parameter property="types" javaType="string" /> <!-- really an 
array of string -->
    </parameterMap>
    
    <statement id="create-data-table" 
parameterMap="create-data-table-param">
        CREATE TABLE data
            <!-- what would be the name to set in the property attribute 
? -->
            <iterate property="???" open="(" close=")" conjunction=",">
                <!-- I want the names and types collections to be 
iterated in synch -->
                $names[]$ $types[]$
            </iterate>
    </statement>

Nested iterate statements won't do it because it produce n*n outputs 
instead of just n (n is the number of elements in the collections).

Any help would be appreciated.




Re: How to iterate over two (or more) collections simultaneously ?

Posted by Jeff Butler <je...@gmail.com>.
Simple answer - no.

You could make a List of HashMaps to accomplish this:

<iterate property="listOfMaps" open="(" close=")" conjunction=",">
    $listOfMaps[].name$ $listOfMaps[].type$
</iterate>

It's a little more work on the Java side of things, but it will work.

Jeff Butler




On Wed, Oct 22, 2008 at 5:18 PM, Pascal Jacob <za...@wanadoo.fr> wrote:
> Hi again,
>
> Is it possible to iterate over two (or more) collections SIMULTANEOUSLY
> using the same indice ?
>
> Here is a example:
>
>   <parameterMap id="create-data-table-param" class="java.util.HashMap">
>       <parameter property="names" javaType="string" /> <!-- really an array
> of string -->
>       <parameter property="types" javaType="string" /> <!-- really an array
> of string -->
>   </parameterMap>
>     <statement id="create-data-table"
> parameterMap="create-data-table-param">
>       CREATE TABLE data
>           <!-- what would be the name to set in the property attribute ? -->
>           <iterate property="???" open="(" close=")" conjunction=",">
>               <!-- I want the names and types collections to be iterated in
> synch -->
>               $names[]$ $types[]$
>           </iterate>
>   </statement>
>
> Nested iterate statements won't do it because it produce n*n outputs instead
> of just n (n is the number of elements in the collections).
>
> Any help would be appreciated.
>
>
>
>