Java 8 optionally replace return null

I am revising Java 8 code and I want to replace null checks with an option.

public Employee findEmployeeById(String id) { List<Employee> empList = .. //some db query return (empList.isEmpty() ? null : empList.get(0)); } 

Optional.ofNullable(empList.get(0)) will not work as if it IndexOutofBoundException

Or should I ideally replace zero with Optional.empty() ?

+5
source share
4 answers

As mentioned in the comments of @Jesper, you should check to see if it is empty and then return an empty Optional .

 public Optional<Employee> findEmployeeById(String id) { List<Employee> empList = .. //some db query return empList.isEmpty() ? Optional.empty() : Optional.of(empList.get(0)); } 

An Optional is a shell with a potentially null value that avoids explicitly checking for null when using it.

See additional documentation to find out what features it provides.

For example, you can get the name of the employee or "unknown" if it is missing, without checking for null :

 Optional<Employee> emp = findEmployeeById(id); String name = emp.map(Employee::getName).orElse("unknown"); 

You can read this post about Uses for Optional to find out if it makes sense to use Optional .

+9
source

Would it be natural for me to keep the design ? : ? : as in Answers on flora . If, however, you want to get rid of this, there is an elegant solution without it:

 public Optional<Employee> findEmployeeById(String id) { List<Employee> empList = .. //some db query return empList.stream().findFirst(); } 

This gives you what you want, because findFirst() returns Optional . If you do not care about which element you get, or know that it is not more than one, you can use findAny() , it also returns Optional .

+5
source

Why don't you just replace your method:

 public Optional<Employee> findEmployeeById(String id) { List<Employee> empList = .. //some db query return (empList.isEmpty() ? Optional.empty() : Optional.ofNullable(empList.get(0))); } 

I suggest you wrap empList.get(0) in Optional.ofNullable if it can still be empty.

How much better is it: think of the calling method. Whoever calls, your method should think what to actually do when the result is empty .

In addition, now you force the code, for example:

 Optional<Employee> emp = findEmployeeById("12"); if (emp.isPresent()) { } else { .... } 

You can also link this to become more free:

 emp.orElseThrow(RuntimeException::new) 

Or other optional methods.

This is simply not the case when you return an Employee. You don’t even think (usually) to check if the link is null.

This makes your code less error prone and more understandable.

+3
source

Another possibility would be to:

 return Optional.of(empList).filter(list -> !list.isEmpty()).map(list -> list.get(0)); 

This will automatically return an empty Optional if the list is empty or empList.get(0) returns null .

If empList can be null , use Optional.ofNullable(empList) instead of Optional(empList) .

+2
source

All Articles