Mapstruct List to List: mapping element list position

  Kiến thức lập trình

I have to classes Source and Target, where Target has an additional field index, and I want to map a list of Source objects to a list of Target objects, where the index field is populated with the position of the corresponding Source object in the source list.

I have a working example utilizing guava:

List<Target> mapList(List<Source> sources) {
    return Streams.mapWithIndex(sources.stream(), (source, index) -> mapElement(source, (int) index)).toList();
}

abstract Target mapElement(Source source, int index);

Since I want to get rid of guava, I wonder if there is a mapstruct way of achieving the same functionality.

I was researching about mapstruct functionality, but I found that there is no documented way of achieving the result. Now maybe there is some trickery that does the job.

LEAVE A COMMENT