title: Lambda expression in java 8 (Null check in HashMap using java.util.Optional)
body: <p>I'm new to java 8 and learning lambda expression.</p>
  
  <p>I have an interface.</p>
  
  <p>Configuration.java</p>
  
  <pre><code>package com.raghu.example;
  
  import java.util.Map;
  
  
  public interface Configuration {
  
      public Map&lt;String,String&gt; getPayload();
  
  }
  </code></pre>
  
  <p>Impl for interface: ConfigurationImpl.java</p>
  
  <pre><code>package com.raghu.example;
  
  import java.util.HashMap;
  import java.util.Map;
  
  public class ConfigurationImpl implements Configuration {
  
      @Override
      public Map&lt;String, String&gt; getPayload() {
          Map&lt;String,String&gt; map = new HashMap&lt;&gt;();
          map.put("key1", "value1");
          map.put("key2", "value2");
          //return null;
          return map;
      }
  
  }
  </code></pre>
  
  <p>My Main Program : Sample.java</p>
  
  <pre><code>package com.raghu.example;
  import java.util.Optional;
  
  public class Sample {
  
      Configuration config;
  
      /*
       * if config is null, return "";
       * if config.getPaylaod is null, return "";
       * if config.payload is not null and if key value is present, then return the value
       * if config.payload is not null and if invalid key, then return "";
       * 
       */
  
      String test(String key) {
          config = new ConfigurationImpl();
          String dataValue = "";
  
          /** java 7 
         if (StringUtils.isNotEmpty(key)) {
              try {
                  String input= config.getPayload()==null?"":config.getPayloadmap().get(key);
                  return (input!=null?input:"");
              } catch (Exception e) {
                  return "";
              }
          }
          return "";
      }**/
          Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v-&gt;{final String str=v.get(key);System.out.println(str);});
  //        //cdataValue=str;});
  //        //Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v-&gt;{final String str=v.get(key);System.out.println(str);return str;});
         return dataValue;
      }
  
      public static void main(String[] args) {
         Sample s = new Sample();
         String value="";
         value=s.test("key"); //pass invalid , should return "";
         System.out.println(value);
         value=s.test("key1"); //pass invalid , should return actual value
         System.out.println(value);
      }
  
  }
  </code></pre>
  
  <p>Now in the method test, i want to do the following[""].</p>
  
  <ol>
  <li>Check if config is null, return empty String.</li>
  <li>Check if collection is null, return empty string.</li>
  <li>If collection is not null, and if key is present, then return the value else return "";</li>
  </ol>
  
  <p>How to do this in java 8?</p>
  
  <p>Tried this:</p>
  
  <ol>
  <li><p>Error 1:Void methods cannot return a value</p>
  
  <pre><code>Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v-&gt;{final String str=v.get(key);System.out.println(str);return str;});
  </code></pre></li>
  <li><p>Error 2: Local variable dataValue defined in an enclosing scope must be final or effectively final.</p>
  
  <pre><code>Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v-&gt;{final String str=v.get(key);System.out.println(str);dataValue= str;});
  </code></pre></li>
  </ol>
  
  <p>Adding final doesn't solve.</p>
  
  <p>Also posted the java 7 code and I'm trying to solve the if-else part of java 7 with lambda expression.</p>