我必须对 Source 和 Target 进行分类,其中 Target 有一个附加字段索引,并且我想将 Source 对象列表映射到 Target 对象列表,其中索引字段由对应 Source 对象在来源列表。
我有一个使用番石榴的工作示例:
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);
由于我想摆脱番石榴,我想知道是否有一种mapstruct方法可以实现相同的功能。
我正在研究mapstruct功能,但我发现没有记录的方法来实现结果。现在也许有一些技巧可以完成这项工作。
如果你不真正使用索引:
@Mapper
public interface Mapper {
List<Target> map(List<Source> sources);
}
仅此而已