[ListUtils.lazyList] create on demand elements with lazyList

When the index passed to the returned list’s get method is greater than the list’s size, then the factory will be used to create a new object and that object will be inserted at that index.

For example,

List<Date> list = ListUtils.lazyList(new ArrayList<Date>(), new Factory() {
public Object create() {
return new Date();
}
});
Date d = list.get(3);

After the above code is executed, obj will contain a new Date instance. Furthermore, that Date instance is the fourth element in the list. The first, second, and third element are all set to null.

We use this in template. Instead of declare it using normal java array, the lazylist object is used. Let’s see another example that is not related with above example,

<forEach items="${invoice.lineItems}" varStatus="i">
<form:input path="lineItems[${i.index}].productName" />
</c:forEach>

This will solve problem when user submit the form.

ref : apache, eggsy84

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s