title: Java 8 Optional, multiple filter and multiple map
body: <p>I would like to use Java 8, how can I make this imperative style to functional style in java 8 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html" rel="nofollow">Optional</a> ?</p>
  
  <p>For instance, let's say <strong>exchange.getIn().getBody()</strong> maybe null or not.</p>
  
  <pre><code>String color = null;
  if(exchange.getIn().getBody() instanceof Foo) {
      color = "toFoo";
  } else if(exchange.getIn().getBody() instanceof Bar){
      color = "toBar";
  }
  </code></pre>
  
  <p>Maybe something thing like this, but I need it to be in one liner.</p>
  
  <pre><code> Optional.ofNullable(exchange.getIn().getBody())
           .filter(body -&gt; body instanceof Foo)
           .map(body -&gt;  "toFoo");
  </code></pre>
  
  <p>My problem is that map already a String, so I was not able to make it one liner:</p>
  
  <pre><code> Optional.ofNullable(exchange.getIn().getBody())
           .filter(body -&gt; body instanceof Foo)
           .map(body -&gt;  "toFoo")
           .orElse(exchange.getIn().getBody()) //this part is problem, already String
           .filter(body -&gt; body instanceof Bar)
           .map(body -&gt; "toBar");
  </code></pre>