简体   繁体   English

使用 Java 8 生成 dto 列表到实体列表

[英]Generate list of dtos to the list of entities using Java 8

I'm new to Java.我是 Java 的新手。 I have list of dto objects and I need to convert it to the list of entities by iterating through dtos list.我有 dto 对象列表,我需要通过迭代 dto 列表将其转换为实体列表。

I should not use model mapper or MapStruct or BeanUtils.我不应该使用 model 映射器或 MapStruct 或 BeanUtils。 I need to do this in the Java way, and I'm not sure how to iterate both lists at the same time.我需要以 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;
    }

It should be replacing Green with Grove and BR with Tampa only.它应该只用 Grove 代替 Green,用 Tampa 代替 BR。 Remaining object such as "28", "13" should be unchanged.其余 object 如“28”、“13”应保持不变。

Nowadays, the Java way is to use Stream API .如今,Java 的方式是使用Stream API

Here's a snippet on how to convert a List<AddressDto> to a List<AddressEntity>这是关于如何将List<AddressDto>转换为List<AddressEntity>的片段

  private static List<AddressEntity> convertDtoToEntity(List<AddressDto> aDto) {
    return aDto.stream()
        .map(dto -> new AddressEntity(dto.getUnitNo(), dto.getFloorNo(), dto.getBuildingName(), dto.getAreaName()))
        .collect(Collectors.toList());
  }

The function passed to the .map method is the one responsible of converting each element of the stream from AddressDto to AddressEntity.传递给.map方法的 function 负责将 stream 的每个元素从 AddressD 转换为 AddressEntity。

You have received a great answer, but here's another way to do it:您已经收到了很好的答案,但这是另一种方法:

private List<AddressEntity> convertDtoToEntity(List<AddressDto> aDto) {
    List<AddressEntity> newAddressEntityList = new ArrayList<>();

    for (AddressDto dto : aDto) {
        AddressEntity addressEntity = new AddressEntity(
                dto.getUnitNo(),
                dto.getFloorNo(),
                dto.getBuildingName(),
                dto.getAreaName()
        );
        
        newAddressEntityList.add(addressEntity);
    }

    return newAddressEntityList;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM