Raghu Raghu - 5 months ago 24
Java Question

Lambda expression in java 8 (Null check in HashMap using java.util.Optional)

I'm new to java 8 and learning lambda expression.

I have an interface.


package com.raghu.example;

import java.util.Map;

public interface Configuration {

public Map<String,String> getPayload();


Impl for interface: ConfigurationImpl.java

package com.raghu.example;

import java.util.HashMap;
import java.util.Map;

public class ConfigurationImpl implements Configuration {

public Map<String, String> getPayload() {
Map<String,String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
//return null;
return map;


My Main Program : Sample.java

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->{final String str=v.get(key);System.out.println(str);});
// //cdataValue=str;});
// //Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v->{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 "";
value=s.test("key1"); //pass invalid , should return actual value


Now in the method test, i want to do the following[""].
1. Check if config is null, return empty String.
2. check if collection is null, return empty string.
3. if collection is not null, and if key is present, then return the value else return "";

How to do this in java 8. I'm new to java 8.

Tried this:
1. Error 1:Void methods cannot return a value

Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v->{final String str=v.get(key);System.out.println(str);return str;});

  1. Error 2: Local variable dataValue defined in an enclosing scope must be final or effectively final.

    Optional.ofNullable(config).map(Configuration::getPayload).ifPresent(v->{final String str=v.get(key);System.out.println(str);dataValue= str;});

Adding final doesnt solve.

Please let me know if i need to provide more information.

Also posted the java 7 code and i'm trying to solve the if-else part of java 7 with lambda expression.


You can just keep on chaining .map(...). If null happens at any of the steps, it will just make the whole thing into an empty Optional and then orElse("") will return the default value.

return Optional.ofNullable(config)