Java / MapStruct Java Interview questions part 2
How do you map a List of objects with MapStruct?
You simply declare a mapper method whose parameter and return types are the collection types you need; MapStruct detects the generic element types and generates a loop that maps each element using the appropriate bean mapping method.
@Mapper public interface BookMapper { BookDto toDto(Book book); List<BookDto> toDtoList(List<Book> books); }
Internally the generated code creates a new ArrayList, iterates the source list, calls toDto for each Book, and adds the result. If a matching single-element mapping method already exists (like toDto above), MapStruct reuses it instead of inlining new mapping code.
More Related questions...