Nitesh Virani
Nitesh Virani

Reputation: 1712

Update list of Master object with list of Sub object

I have Deal object and it has subDeals (list of Deal), I need to update the subDeals. DealDTO object has list of subCodes so I to need find the Deal object for each subCodes and update the subDeals into Deal.

Here is the Deal class:

public class Deal {
    String code;
    BigDecimal price;
    List<Deal> subDeals;
    public Deal(String code, BigDecimal price) {
        super();
        this.code = code;
        this.price = price;
        this.subDeals = new ArrayList<>();
    }
    // geteer setter
    public void addSubDeal(Deal subDeal) {
        subDeals.add(subDeal);
    }
}

Here is the DealDTO class

public class DealDTO {
    private String masterCode;
    private List<String> subCodes;
    public DealDTO(String masterCode, List<String> subCodes) {
        super();
        this.masterCode = masterCode;
        this.subCodes = subCodes;
    }
    // geteer setter
}

Below is the approach I am trying to but it is not working:

List<Deal> masterDeals =
            Arrays.asList(
                new Deal("ABC",new BigDecimal(5)),
                new Deal("DEF",new BigDecimal(10)),
                new Deal("GHI",new BigDecimal(15))
                );

    List<DealDTO> dealDTOs =
            Arrays.asList(
                new DealDTO("ABC", Arrays.asList("JKL")),
                new DealDTO("DEF", Arrays.asList("JKL", "ABC")),
                new DealDTO("GHI", Arrays.asList("MNO"))
                );

DealDTO dealDTO = null;

for(Deal masterDeal : masterDeals) {
    // get DealDTO which matches with masterDeal's code
    dealDTO = dealDTOs.stream()
            .filter(dto -> dto.getMasterCode().equals(masterDeal.getCode()))
            .findFirst()
            .get();
    // if subCodes are present then traverse through it and get the Deal object from masterDeals list and update the masterDeal
    if(dealDTO.getSubCodes() != null && !dealDTO.getSubCodes().isEmpty()) {
        dealDTO.getSubCodes().stream().forEach( code -> {
            for(Deal deal : masterDeals) {
                if(deal.getCode().equals(code)) {
                    masterDeal.addSubDeal(deal);
                }
            }
        });
    }
    // subDeals has been added so persist masterDeal here
}
masterDeals.forEach(System.out::println);  // throws java.lang.StackOverflowError

Lists are huge so I wanted to make sure that I follow the right approach to update the subDeals.

Upvotes: 1

Views: 90

Answers (1)

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

There are couple of things that you can do for improving the performance since you are mentioning that the lists are huge.

  1. Use a Map to keep the deals instead of a List. Given a code you should be able to retrieve a deal fast.
  2. If possible, let the Deal also save only codes for subdeals. Provide a getter List<Deal> getSubDeals that creates the list when needed and returns.

Upvotes: 2

Related Questions