2

I have following code:

final Optional<List<ServiceAttributeValue>> attributeValueList = Optional.<Product> of(product)
         .map(Product::getServiceAttributes)
         .map(ServiceAttributeMap::getMap)
         .map((v) -> (ServiceAttribute) v.get(attributeV.getAttributeString()))
         .map((c) -> (List<ServiceAttributeValue>) c.getValueList());

do I need to add check if v is null?

1
  • 3
    You probably don't need the .<Product> and you don't need the prentheses around single argument lambdas (you can replace (v) -> with v ->). Commented Sep 9, 2015 at 20:40

1 Answer 1

2

First of all, your code is buggy. You using Optional to avoid NPE where your code will throw NPE if product is null. You should use Optional.ofNullable instead.

Optional.ofNullable(product)

and the answer is no, the third map will not get executed if ServiceAttributeMap::getMap returns null

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

1 Comment

Why do you think his product can be null? Author did not say so. See this question.

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.