0

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?

6
  • Please post your error message. Commented Nov 26, 2021 at 8:17
  • Hi, I did not run the code so did not any error msg as such. I was getting error from the IDE itself. in the .filter() despCostChangeDtl is a list. so despCostChangeDtl.getItems() is invalid we need to loop through. I am facing issue while fixing this part. Commented Nov 26, 2021 at 8:20
  • Have you tried changing despCostChangeDtl to literally anything else inside your call to filter(), and prepending a call to stream() before you call filter()? Commented Nov 26, 2021 at 8:28
  • Can you elaborate? Sorry for being such a dumb. Commented Nov 26, 2021 at 8:31
  • I think you will have to split this into 2 steps. Collect all your List<CostChangeDtl> in step 1. Then apply filter over all the items in step 2. I will post an answer separately because it difficult to write it here. Commented Nov 26, 2021 at 8:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

After map opt, stream was Stream<List<CostChangeDtl>>. Use flatMap(List::Stream) change the stream to Stream<CostChangeDtl>.
That seems to have resolved the error at least it is not giving red marks in the IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.