# Class LazyList<E>

java.lang.Object
org.apache.commons.collections4.collection.AbstractCollectionDecorator<E>
org.apache.commons.collections4.list.AbstractListDecorator<E>
org.apache.commons.collections4.list.AbstractSerializableListDecorator<E>
org.apache.commons.collections4.list.LazyList<E>

Type Parameters:
  `E` - the type of the elements in the list.

All Implemented Interfaces:
  `Serializable`, `Iterable<E>`, `Collection<E>`, `List<E>`

public class LazyList<E> extends AbstractSerializableListDecorator<E>
Decorates another `List` to create objects in the list on demand.

When the `get(int)` method is called with an index greater than the size of the list, the list will automatically grow in size and return a new object from the specified factory or transformer. The gaps will be filled by null. If a get method call encounters a null, it will be replaced with a new object from the factory. Thus this list is unsuitable for storing null objects.

For instance:

     Factory<Date> factory = new Factory<Date>() {
         public Date create() {
             return new Date();
         }
     }
     List<Date> lazy = LazyList.decorate(new ArrayList<Date>(), factory);
     Date date = lazy.get(3);

After the above code is executed, `date` 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`.

This class differs from `GrowthList` because here growth occurs on get, where `GrowthList` grows on set and add. However, they could easily be used together by decorating twice.

This class is Serializable from Commons Collections 3.1.

Since:
  3.0
See Also:
  - `GrowthList`
  - Serialized Form

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `LazyList(List<E> list, Factory<? extends E> factory)`
  Constructor that wraps (not copies).
  `protected`
  `LazyList(List<E> list, Transformer<Integer,? extends E> transformer)`
  Constructor that wraps (not copies).

- ## Method Summary

  Modifier and Type
  Method
  Description
  `E`
  `get(int index)`
  Decorate the get method to perform the lazy behavior.
  `static <E> LazyList<E>`
  `lazyList(List<E> list, Factory<? extends E> factory)`
  Factory method to create a lazily instantiating list.
  `static <E> LazyList<E>`
  `lazyList(List<E> list, Transformer<Integer,? extends E> transformer)`
  Transformer method to create a lazily instantiating list.
  `List<E>`
  `subList(int fromIndex, int toIndex)`

  ### Methods inherited from class org.apache.commons.collections4.list.AbstractListDecorator

  `add, addAll, decorated, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, remove, set`

  ### Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator

  `add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, removeIf, retainAll, setCollection, size, toArray, toArray, toString`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface java.util.Collection

  `parallelStream, removeIf, stream`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

  ### Methods inherited from interface java.util.List

  `add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, replaceAll, retainAll, size, sort, spliterator, toArray, toArray`

- ## Constructor Details

  - ### LazyList

    protected LazyList(List<E> list, Factory<? extends E> factory)
    Constructor that wraps (not copies).
    Parameters:
      `list` - the list to decorate, must not be null
      `factory` - the factory to use for creation, must not be null
    Throws:
      `NullPointerException` - if list or factory is null

  - ### LazyList

    protected LazyList(List<E> list, Transformer<Integer,? extends E> transformer)
    Constructor that wraps (not copies).
    Parameters:
      `list` - the list to decorate, must not be null
      `transformer` - the transformer to use for creation, must not be null
    Throws:
      `NullPointerException` - if list or transformer is null

- ## Method Details

  - ### lazyList

    public static <E> LazyList<E> lazyList(List<E> list, Factory<? extends E> factory)
    Factory method to create a lazily instantiating list.
    Type Parameters:
      `E` - the type of the elements in the list
    Parameters:
      `list` - the list to decorate, must not be null
      `factory` - the factory to use for creation, must not be null
    Returns:
      a new lazy list
    Throws:
      `NullPointerException` - if list or factory is null
    Since:
      4.0

  - ### lazyList

    public static <E> LazyList<E> lazyList(List<E> list, Transformer<Integer,? extends E> transformer)
    Transformer method to create a lazily instantiating list.
    Type Parameters:
      `E` - the type of the elements in the list
    Parameters:
      `list` - the list to decorate, must not be null
      `transformer` - the transformer to use for creation, must not be null
    Returns:
      a new lazy list
    Throws:
      `NullPointerException` - if list or transformer is null
    Since:
      4.4

  - ### get

    public E get(int index)
    Decorate the get method to perform the lazy behavior.

    If the requested index is greater than the current size, the list will grow to the new size and a new object will be returned from the factory or transformer. Indexes in-between the old size and the requested size are left with a placeholder that is replaced with a factory or transformer object when requested.

    Specified by:
      `get` in interface `List<E>`
    Overrides:
      `get` in class `AbstractListDecorator<E>`
    Parameters:
      `index` - the index to retrieve
    Returns:
      the element at the given index

  - ### subList

    public List<E> subList(int fromIndex, int toIndex)
    Specified by:
      `subList` in interface `List<E>`
    Overrides:
      `subList` in class `AbstractListDecorator<E>`