Deepjyoti De
Deepjyoti De

Reputation: 127

Error in lambda function in Java while handling list

I have recently switched from C++ to Java and facing tough times to deal with the syntax and lambda functions. So there is one method which got updated. I am confused editing the code correspondingly.

So initially getCostChangeDtl() used to return data of type CostChangeDtl but now the same method returns a list of CostChangeDtl. So I made some changes accordingly still facing some issues.

Before

costChangeDetails.forEach(
            costChangeDetail -> changeDtls.addAll(costChangeDetail.getItems().stream().map(item -> {
                CostChangeDtl despCostChangeDtl = getCostChangeDtl(ccd, costChangeDetail, item,
                    metrics);
                        
                            if (despCostChangeDtl.getItemID() == null)
                            {
                                String consumerGtin = item.item.getConsumerDetails().getConsumerGtin();
                                missingSkuGtins.add(
                                        format("%s | %s ", consumerGtin, costChangeDetail.getLocation().getId()));
                            }
                        
                        return despCostChangeDtl;
                    }).filter(despCostChangeDtl -> despCostChangeDtl.getItemID() != null)
                .collect(Collectors.toList())));

After

costChangeDetails.forEach(
            costChangeDetail -> changeDtls.addAll(costChangeDetail.getItems().stream().map(item -> {
                List<CostChangeDtl> despCostChangeDtl = getCostChangeDtl(ccd, costChangeDetail, item,
                    metrics);
                        for (CostChangeDtl costChangeDtl : despCostChangeDtl) {
                            if (costChangeDtl.getItemID() == null)
                            {
                                String consumerGtin = item.item.getConsumerDetails().getConsumerGtin();
                                missingSkuGtins.add(
                                        format("%s | %s ", consumerGtin, costChangeDetail.getLocation().getId()));
                            }
                        }
                        return despCostChangeDtl;
                    }).filter(despCostChangeDtl -> despCostChangeDtl.getItemID() != null)
                .collect(Collectors.toList())));

Now I am getting an error in despCostChangeDtl.getItemID() as expected since it is despCostChangeDtl is now a list. How to fix this particular issue?

Upvotes: 0

Views: 205

Answers (1)

rongfeiyue
rongfeiyue

Reputation: 149

despCostChangeDtl from an object to a list, so you should flatmap after map. enter image description here

Upvotes: 1

Related Questions