title: Java Lambda - How to insert a conditional within a complicated nested lamda
body: <p>I have some existing code where I need to add a conditional. In the below, if the value of &quot;at&quot; is &quot;FOOBAR&quot;, then instead of &quot;accessTypeHit.right.getSortValues()&quot; I need &quot;accessTypeHit.right.getSourceAsString()&quot;. Otherwise, it should still be &quot;accessTypeHit.right.getSortValues()&quot;.</p>
  <p>Due to the complexity of the nested lambda, I am having difficulty figuring out how to address this. I would be grateful for any ideas. thanks.</p>
  <pre><code>public class DSHTAFunction implements Function&lt;SearchHits, List&lt;ImmutablePair&lt;String, Set&lt;AObject&gt;&gt;&gt;&gt; {
  @Override
  public List&lt;ImmutablePair&lt;String, Set&lt;AObject&gt;&gt;&gt; apply(@NonNull SearchHits searchHits) {
  
  
      return StreamSupport.stream(searchHits.spliterator(), false).map(searchHit -&gt; {
                  String id = searchHit.getFields().get(ID_FIELD).getValue();
                  Set&lt;AObject&gt; AObjects = Sets.newHashSet();
  
                  AObjects.addAll(ATM.INSTANCE.getTypes().stream()
                                               .flatMap(at -&gt; {
                                                  if (searchHit.getInnerHits() == null) {
                                                      return Stream.empty();
                                                  }
  
                                                  return Arrays.stream(searchHit.getInnerHits().getOrDefault(at.getIFName(), SearchHits.empty())
                                                                               .getHits()).map(h -&gt; ImmutablePair.of(at, h));})
  
                                               .flatMap(accessTypeHit -&gt;
                                                                Arrays.stream(accessTypeHit.right.getSortValues())
                                                                        .filter(sv -&gt; sv != null)
                                                                        .map(sv -&gt; new AObject(accessTypeHit.left, sv.toString())))
                                               .filter(AObject::isNonDefault).collect(Collectors.toSet()));
  
  
                  return ImmutablePair.of(id, AObjects);
              }).collect(Collectors.toList());
  
  }
  </code></pre>