title: Java 8 lambda null check usage
body: <p>I have a scenario where I am checking multiple attributes inside a class for null check.If its not null , I am calling a method which will create me a new object and I need to capture this instance against a reference.I am succesasfully able to do null check using maps but unable to write code which will help me in capturing the return object after invocation.Can someone please help ?</p>
  
  <pre><code>    private WorkflowPreference buildWfPrefDetails(CcarReportPreferenceConfig ccarReportPreferenceConfig) {
      WorkflowPreference workflowPreference = new WorkflowPreference();
      List&lt;PayloadEntry&gt; payLoadEntries = new ArrayList&lt;PayloadEntry&gt;();
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRwpRole)
          .map(RwpRole::getRoleName)
          .ifPresent(s -&gt; workflowPreference.setKey(s));
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSequenceNumber)
          .ifPresent(s -&gt; buildPayLoadEntry("SEQ_NUM", s));
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSlaType)
          .ifPresent(s -&gt; buildPayLoadEntry("SLA_TYPE", s));
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSlaValue)
          .ifPresent(s -&gt; buildPayLoadEntry("SLA_VALUE", s.toString()));
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRejectSequence)
          .ifPresent(s -&gt; buildPayLoadEntry("REJECT_SEQUENCE", s));
      Optional.of(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRaiseQueryNotification)
          .ifPresent(s -&gt; buildPayLoadEntry("RAISE_QUERY_NOTIFICATION", s));
      workflowPreference.getValue().addAll(payLoadEntries);
      return workflowPreference;
  }
  
  private PayloadEntry buildPayLoadEntry(String key, String value) {
      PayloadEntry payloadEntry = new PayloadEntry();
      payloadEntry.setKey(key);
      payloadEntry.setValue(value);
      return payloadEntry;
  }
  </code></pre>
  
  <p>Expected Now :</p>
  
  <p>payloadEntries list should capture the returned object after each buildPayloadEntry call.I want something like below to be implemented along with lambda null checks</p>
  
  <pre><code>payLoadEntries.add(buildPayLoadEntry("SEQ_NUM", ccarReportPreferenceConfig.getSequenceNumber()));
  payLoadEntries.add(buildPayLoadEntry("SLA_TYPE", ccarReportPreferenceConfig.getSlaValue().toString()));
  </code></pre>
  
  <p>Updated Answer as per below suggestions</p>
  
  <pre><code>Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRwpRole)
          .map(RwpRole::getRoleName)
          .ifPresent(s -&gt; workflowPreference.setKey(s));
      Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSequenceNumber)
          .ifPresent(s -&gt; payLoadEntries.add(buildPayLoadEntry("SEQ_NUM", s)));
      Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSlaType)
          .ifPresent(s -&gt; payLoadEntries.add(buildPayLoadEntry("SLA_TYPE", s)));
      Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getSlaValue)
          .ifPresent(s -&gt; payLoadEntries.add(buildPayLoadEntry("SLA_VALUE", s.toString())));
      Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRejectSequence)
          .ifPresent(s -&gt; payLoadEntries.add(buildPayLoadEntry("REJECT_SEQUENCE", s)));
      Optional.ofNullable(ccarReportPreferenceConfig)
          .map(CcarReportPreferenceConfig::getRaiseQueryNotification)
          .ifPresent(s -&gt; payLoadEntries.add(buildPayLoadEntry("RAISE_QUERY_NOTIFICATION", s)));
      workflowPreference.getValue().addAll(payLoadEntries);
  </code></pre>