【发布时间】:2021-08-13 17:46:43
【问题描述】:
我是 Java 新手。我有 dto 对象列表,我需要通过迭代 dto 列表将其转换为实体列表。
我不应该使用模型映射器或 MapStruct 或 BeanUtils。我需要用 Java 的方式来做这件事,我不确定如何同时迭代两个列表。
public class AddressDto {
private String unitNo;
private String floorNo;
private String buildingName;
private String areaName;
//getters and setters
}
public class AddressEntity {
private String unitNo;
private String floorNo;
private String buildingName;
private String areaName;
//getters and setters
}
public void getAddress() {
List<AddressDto> addressDtoList=new ArrayList<>();
addressDtoList.add(new AddressDto("174", "7", "Grove", "BR"));
List<AddressEntity> addressEntityList=new ArrayList<>();
addressEntityList.add(new AddressEntity("28", "13", "Green", "Tampa"));
List<AddressEntity> addressEntityListResult=convertDtoToEntity(addressDtoList);
}
private List<AddressEntity> convertDtoToEntity(List<AddressDto> aDto) {
List<AddressEntity> newAddressEntityList = null;
for (AddressDto dto : aDto) {
//Generate and Return the newAddressEntityList by replacing Green with Grove and BR with Tampa
}
return newAddressEntityList;
}
应该只用 Grove 代替 Green,用 Tampa 代替 BR。其余对象如“28”、“13”应保持不变。
【问题讨论】:
标签: java spring-boot arraylist foreach java-8