Start by writing the map method using only the Iterator, then explain how generics allow different input and output types. Finally, discuss eager vs. lazy evaluation and the implications of laziness with source list mutation.
Pro tip: Mention that lazy evaluation can cause unexpected behavior if the source list is mutated after map is called, and suggest documenting or defending against it.
Create a new List, obtain an Iterator from the source list, and iterate while hasNext() is true, applying fn to each element and adding the result to the new list.
Explain that map should be generic: <R> List<R> map(Function<T, R> fn), allowing input type T and output type R to differ.
Eager evaluation applies fn immediately and returns a new list; lazy evaluation returns a view that applies fn on demand, saving memory and computation if not all elements are needed.
If lazy, the view reflects changes to the source list after map is called, which can lead to inconsistent or surprising results; discuss trade-offs and possible solutions like copying or documenting behavior.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.