Search code examples
mapstruct

How to map java collection to its size


I would like to know how I can map for example List to its size? I have some SourseClass.class containing List of something and I would like to map this List only to its size so DtoClass.class would contain "List.size()"


Solution

  • You can use expression for that, here is example:

    @Mapping(target = "list", expression = "java(sourceClass.getList()!= null ? sourceClass.getList().size():0)")
    DtoClass toDto(SourceClass sourceClass);