I am using a Map<String, Optional<List<String>>>. I am getting an obvious NullPointerException because the result is null for that key.
Is there any way to handle the null situation?
public Map<MyEnum, Optional<List<String>>> process(Map<MyEnum, Optional<List<String>>> map) {
Map<MyEnum, Optional<List<String>>> resultMap = new HashMap<>();
// Getting NullPointerException here, since map.get(MyEnum.ANIMAL) is NULL
resultMap.put(MyEnum.ANIMAL, doSomething(map.get(MyEnum.ANIMAL).get()));
// do something more here
}
private Optional<List<String>> doSomething(List<String> list) {
// process and return a list of String
return Optional.of(resultList);
}
I am trying to avoid a if-else check for null by using Optional.