You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2010/11/17 22:15:07 UTC

Mutating a collection

All,

I'd like to mutate a collection during template evaluation --
essentially, I'd like to call Iterator.remove() during a loop. Is that
possible? Does Velocity expose an Iterator or anything like that to the
template?

Or, am I forced to iterate over one collection and add to another one?

For that matter, is there a convenient way of creating an empty
ArrayList or something similar?

Thanks,
-chris


Re: Mutating a collection

Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, Nov 17, 2010 at 1:59 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> Nathan,
>
> On 11/17/2010 4:44 PM, Nathan Bubna wrote:
>> On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz
>> <ch...@christopherschultz.net> wrote:
>>> All,
>>>
>>> I'd like to mutate a collection during template evaluation --
>>> essentially, I'd like to call Iterator.remove() during a loop. Is that
>>> possible? Does Velocity expose an Iterator or anything like that to the
>>> template?
>>
>> #set( $iterator = $list.iterator() )
>> #foreach( $item in $iterator )
>> #if ($item.isBad() )#set( $null = $iterator.remove() )#end
>> #end
>
> Duh. I was under the mistaken impression that the documentation
> (http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html#Loops)
> was accurate when I actually know better: you can loop over an iterator
> directly!

Yeah, it'll probably put an annoying warning in the logs about it, but
it works fine.  It's not the kind of thing we typically want to
document, because it's not the kind of thing that people should
generally do in the view layer.  But, we always support more than we
ever recommend around here. :)

>>> Or, am I forced to iterate over one collection and add to another one?
>>>
>>> For that matter, is there a convenient way of creating an empty
>>> ArrayList or something similar?
>>
>> #set( $list = [] )
>
> Is that documented anywhere? I'm not shocked that I get some kind of
> collection from that, but I don't see anything in the users guide that
> suggests that the above will give me a mutable collection, and if so,
> what kind.

i dunno.  i haven't read through the documentation in years.  i tend
not to need to.  but yes, anytime you create a list in VTL, you are
creating a java.util.ArrayList in all its mutable glory.

> Thanks,
> -chris
>
>

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


Re: Mutating a collection

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Nathan,

On 11/17/2010 4:44 PM, Nathan Bubna wrote:
> On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz
> <ch...@christopherschultz.net> wrote:
>> All,
>>
>> I'd like to mutate a collection during template evaluation --
>> essentially, I'd like to call Iterator.remove() during a loop. Is that
>> possible? Does Velocity expose an Iterator or anything like that to the
>> template?
> 
> #set( $iterator = $list.iterator() )
> #foreach( $item in $iterator )
> #if ($item.isBad() )#set( $null = $iterator.remove() )#end
> #end

Duh. I was under the mistaken impression that the documentation
(http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html#Loops)
was accurate when I actually know better: you can loop over an iterator
directly!

>> Or, am I forced to iterate over one collection and add to another one?
>>
>> For that matter, is there a convenient way of creating an empty
>> ArrayList or something similar?
> 
> #set( $list = [] )

Is that documented anywhere? I'm not shocked that I get some kind of
collection from that, but I don't see anything in the users guide that
suggests that the above will give me a mutable collection, and if so,
what kind.

Thanks,
-chris


Re: Mutating a collection

Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> All,
>
> I'd like to mutate a collection during template evaluation --
> essentially, I'd like to call Iterator.remove() during a loop. Is that
> possible? Does Velocity expose an Iterator or anything like that to the
> template?

#set( $iterator = $list.iterator() )
#foreach( $item in $iterator )
#if ($item.isBad() )#set( $null = $iterator.remove() )#end
#end

> Or, am I forced to iterate over one collection and add to another one?
>
> For that matter, is there a convenient way of creating an empty
> ArrayList or something similar?

#set( $list = [] )

> Thanks,
> -chris
>
>

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


Re: Mutating a collection

Posted by jian chen <ch...@gmail.com>.
One thing I think you could do, use a bitset object to record which item is
to be removed. After the loop, loop over the bit set and remove items from
the collection...Just my 3 cents ;-)

Jian

On Wed, Nov 17, 2010 at 1:27 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> Jian,
>
> On 11/17/2010 4:19 PM, jian chen wrote:
> > It seems to me that in java, Iterator can not be modified during the
> > iteration loop. It is not particular to Velocity though.
>
>
> http://download.oracle.com/javase/6/docs/api/java/util/Iterator.html#remove()<http://download.oracle.com/javase/6/docs/api/java/util/Iterator.html#remove%28%29>
>
> You may need to update your interview questions ;)
>
> > If you want to remove elements, maybe during the loop, you could mark the
> > indexes for the items to be removed, then, remove them afterwards?
>
> That's an idea, though much less convenient than calling "remove" on an
> iterator.
>
> Thanks,
> -chris
>
>

Re: Mutating a collection

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Jian,

On 11/17/2010 4:19 PM, jian chen wrote:
> It seems to me that in java, Iterator can not be modified during the
> iteration loop. It is not particular to Velocity though.

http://download.oracle.com/javase/6/docs/api/java/util/Iterator.html#remove()

You may need to update your interview questions ;)

> If you want to remove elements, maybe during the loop, you could mark the
> indexes for the items to be removed, then, remove them afterwards?

That's an idea, though much less convenient than calling "remove" on an
iterator.

Thanks,
-chris


Re: Mutating a collection

Posted by Barbara Baughman <ba...@utdallas.edu>.
Or put a duplicate of the collection in the context with a different reference name.  During the loop, you can remove the item from the duplicate because you aren't affecting the Iterator of the original.  Then work with the modified duplicate.

Barbara Baughman
Systems Analyst
X2157

On 11/17/10 15:19, jian chen wrote:
> Hi Chris,
>
> It seems to me that in java, Iterator can not be modified during the
> iteration loop. It is not particular to Velocity though.
>
> If you want to remove elements, maybe during the loop, you could mark the
> indexes for the items to be removed, then, remove them afterwards?
>
> Cheers,
>
> Jian
> http://www.jiansnet.com/search?q=computer+science+interview+questions
>
> On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz<
> chris@christopherschultz.net>  wrote:
>
>> All,
>>
>> I'd like to mutate a collection during template evaluation --
>> essentially, I'd like to call Iterator.remove() during a loop. Is that
>> possible? Does Velocity expose an Iterator or anything like that to the
>> template?
>>
>> Or, am I forced to iterate over one collection and add to another one?
>>
>> For that matter, is there a convenient way of creating an empty
>> ArrayList or something similar?
>>
>> Thanks,
>> -chris
>>
>>
>

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


Re: Mutating a collection

Posted by jian chen <ch...@gmail.com>.
Hi Chris,

It seems to me that in java, Iterator can not be modified during the
iteration loop. It is not particular to Velocity though.

If you want to remove elements, maybe during the loop, you could mark the
indexes for the items to be removed, then, remove them afterwards?

Cheers,

Jian
http://www.jiansnet.com/search?q=computer+science+interview+questions

On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> All,
>
> I'd like to mutate a collection during template evaluation --
> essentially, I'd like to call Iterator.remove() during a loop. Is that
> possible? Does Velocity expose an Iterator or anything like that to the
> template?
>
> Or, am I forced to iterate over one collection and add to another one?
>
> For that matter, is there a convenient way of creating an empty
> ArrayList or something similar?
>
> Thanks,
> -chris
>
>