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?

assylias
  • 321,522
  • 82
  • 660
  • 783
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
  • 3
    You probably don't need the `.` and you don't need the prentheses around single argument lambdas (you can replace `(v) ->` with `v ->`). – assylias Sep 09 '15 at 20:40

1 Answers1

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

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 2
    Why do you think his `product` *can* be null? Author did not say so. See [this question](http://stackoverflow.com/q/31696485/4856258). – Tagir Valeev Sep 10 '15 at 01:39