将第二个数组的对象列表与第一个数组中的结构字段合并

时间:2023-01-20 12:11:42

I have two array list of object and i need fill object element in first array list from some of field object element from second array list and this project need best performance please guide me for best way for solve this problem .

我有两个对象的数组列表,我需要从第二个数组列表中的一些字段对象元素填充第一个数组列表中的对象元素,这个项目需要最佳性能请指导我找到解决这个问题的最佳方法。

first array list's object structure ArrayList <SellProductAmountDTO> and fill OrderProductDTO object with conditional sellContractId field of SellProductAmountDTO with id of SellContractProduct.

第一个数组列表的对象结构ArrayList 并使用SellProductAmountDTO的条件sellContractId字段填充OrderProductDTO对象,其id为SellContractProduct。

public class SellProductAmountDTO {
    private Long sellContractId;
    private Long amount;
    private OrderProductDTO orderProduct;
    //getter ans setters
}

and OrderProductDTO stucture :

和OrderProductDTO结构:

public class OrderProductDTO {
    private List<Long> costGroupId;
    private Long productId;
}

and second array list's object stucture ArrayList<SellContractProduct>:

和第二个数组列表的对象结构ArrayList :

public class SellContractProduct{

    private Long id;

    private Long rateGroupId;

    private Long currencyRateGroupId;

    private Set<BuyType> buyTypes;

    private Consumption consumption;

    private Long productId;

    private SellContractCustomer sellContractCustomer;

    private Set<Depot> depots = new HashSet<>();

    private List<Long> costGroupIds;

    private List<Long> currencyIds;

    //getter and setters
}

1 个解决方案

#1


1  

If I understand your problem correctly, this should work:

如果我理解你的问题,这应该工作:

List<SellProductAmountDTO> sellProductAmountDTOList = new ArrayList<>(); // this should be full
List<SellContractProduct> sellContractProductList = new ArrayList<>(); // this should be full

// build a stream on array 2 to use in the for loop
Stream<SellContractProduct> SellContractProductListStream = sellContractProductList.stream();

for(SellProductAmountDTO sellProductAmountDTO : sellProductAmountDTOList) {
    // find the correct SellContractProduct
    Optional<SellContractProduct> productOption = SellContractProductListStream
            .filter(sellContactProduct -> sellContactProduct.getId().equals(sellProductAmountDTO.getSellContractId()))
            .findFirst();

    // if the SellContractProduct was found, then set its values into the sellProductAmountDTO's order product
    if(productOption.isPresent()) {
        sellProductAmountDTO.getOrderProduct().setProductId(productOption.get().getId());
        sellProductAmountDTO.getOrderProduct().setCostGroupId(productOption.get().getCostGroupIds());
    }
}

EDIT Might have better performance with a map

编辑可以使用地图获得更好的性能

List<SellProductAmountDTO> sellProductAmountDTOList = new ArrayList<>(); // this should be full
List<SellContractProduct> sellContractProductList = new ArrayList<>(); // this should be full

// make a map listing contracts by their Id
Map<Long, SellContractProduct> SellContractProductMap = sellContractProductList.stream()
        .collect(Collectors.toMap(SellContractProduct::getId, Function.identity()));

for(SellProductAmountDTO sellProductAmountDTO : sellProductAmountDTOList) {
    // find the correct SellContractProduct in the map
    SellContractProduct product = SellContractProductMap.get(sellProductAmountDTO.getSellContractId());

    if(product != null) {
        sellProductAmountDTO.getOrderProduct().setProductId(product.getId());
        sellProductAmountDTO.getOrderProduct().setCostGroupId(product.getCostGroupIds());
    }
}

#1


1  

If I understand your problem correctly, this should work:

如果我理解你的问题,这应该工作:

List<SellProductAmountDTO> sellProductAmountDTOList = new ArrayList<>(); // this should be full
List<SellContractProduct> sellContractProductList = new ArrayList<>(); // this should be full

// build a stream on array 2 to use in the for loop
Stream<SellContractProduct> SellContractProductListStream = sellContractProductList.stream();

for(SellProductAmountDTO sellProductAmountDTO : sellProductAmountDTOList) {
    // find the correct SellContractProduct
    Optional<SellContractProduct> productOption = SellContractProductListStream
            .filter(sellContactProduct -> sellContactProduct.getId().equals(sellProductAmountDTO.getSellContractId()))
            .findFirst();

    // if the SellContractProduct was found, then set its values into the sellProductAmountDTO's order product
    if(productOption.isPresent()) {
        sellProductAmountDTO.getOrderProduct().setProductId(productOption.get().getId());
        sellProductAmountDTO.getOrderProduct().setCostGroupId(productOption.get().getCostGroupIds());
    }
}

EDIT Might have better performance with a map

编辑可以使用地图获得更好的性能

List<SellProductAmountDTO> sellProductAmountDTOList = new ArrayList<>(); // this should be full
List<SellContractProduct> sellContractProductList = new ArrayList<>(); // this should be full

// make a map listing contracts by their Id
Map<Long, SellContractProduct> SellContractProductMap = sellContractProductList.stream()
        .collect(Collectors.toMap(SellContractProduct::getId, Function.identity()));

for(SellProductAmountDTO sellProductAmountDTO : sellProductAmountDTOList) {
    // find the correct SellContractProduct in the map
    SellContractProduct product = SellContractProductMap.get(sellProductAmountDTO.getSellContractId());

    if(product != null) {
        sellProductAmountDTO.getOrderProduct().setProductId(product.getId());
        sellProductAmountDTO.getOrderProduct().setCostGroupId(product.getCostGroupIds());
    }
}